Skip to content

Commit 2635a25

Browse files
committed
Simpliefied Example and added README
1 parent 58de9d9 commit 2635a25

5 files changed

Lines changed: 16 additions & 37 deletions

File tree

‎SpotifyAPI.Web.Examples.ASP/Controllers/HomeController.cs‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
using Microsoft.AspNetCore.Authentication;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.Extensions.Logging;
76
using SpotifyAPI.Web.Examples.ASP.Models;
87

98
namespace SpotifyAPI.Web.Examples.ASP.Controllers
109
{
10+
[Authorize(AuthenticationSchemes = "Spotify")]
1111
public class HomeController : Controller
1212
{
13-
private readonly ILogger<HomeController> _logger;
14-
15-
public HomeController(ILogger<HomeController> logger)
16-
{
17-
_logger = logger;
18-
}
19-
2013
public async Task<IActionResult> Index()
2114
{
22-
if(!User.Identity.IsAuthenticated)
23-
return Challenge(new AuthenticationProperties { RedirectUri = "/" }, "Spotify");
24-
2515
var accessToken = await HttpContext.GetTokenAsync("Spotify", "access_token");
2616
SpotifyWebAPI api = new SpotifyWebAPI
2717
{
@@ -34,11 +24,6 @@ public async Task<IActionResult> Index()
3424
return View(new IndexModel { SavedTracks = savedTracks });
3525
}
3626

37-
public IActionResult Privacy()
38-
{
39-
return View();
40-
}
41-
4227
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
4328
public IActionResult Error()
4429
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ASP.NET Core Spotify Example
2+
3+
This project provides an example ASP .NET Core Web Application, which utilizes `SpotifyAPI.Web` and `AspNet.Security.OAuth.Spotify` to authenticate the user from Spotify and retrieve his saved library tracks.
4+
5+
## Important files/lines
6+
7+
Most of the important configuration is inside of `Startup.cs`. It reads the `client_id` and `client_secret` from the configration store, so make sure you run the following commands in the project directory:
8+
9+
`dotnet user-secrets set client_secret YOUR_CLIENT_SECRET`
10+
`dotnet user-secrets set client_id YOUR_CLIENT_ID`
11+
12+
Also, it specifies the callback URL (`/callback`). This has to be registered in your Spotify App Settings (`http://localhost:5000/callback`)
13+
14+
The actual request to Spotify and making sure that the user is authenticated happens in `Controllers/HomeController.cs`, which should be self explanatory.

‎SpotifyAPI.Web.Examples.ASP/Startup.cs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ public void ConfigureServices(IServiceCollection services)
2323
services.AddControllersWithViews();
2424

2525
services.AddAuthentication(o => o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
26-
.AddCookie(options =>
27-
{
28-
options.LoginPath = "/signin";
29-
options.LogoutPath = "/signout";
30-
})
26+
.AddCookie()
3127
.AddSpotify(options =>
3228
{
3329
var scopes = Scope.UserLibraryRead;

‎SpotifyAPI.Web.Examples.ASP/Views/Home/Privacy.cshtml‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎SpotifyAPI.Web.Examples.ASP/Views/Shared/_Layout.cshtml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
aria-expanded="false" aria-label="Toggle navigation">
1717
<span class="navbar-toggler-icon"></span>
1818
</button>
19-
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
20-
<ul class="navbar-nav flex-grow-1">
21-
<li class="nav-item">
22-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
23-
</li>
24-
<li class="nav-item">
25-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
26-
</li>
27-
</ul>
28-
</div>
2919
</div>
3020
</nav>
3121
</header>

0 commit comments

Comments
 (0)