diff --git a/SpotifyAPI.Web.Examples.ASP/Controllers/HomeController.cs b/SpotifyAPI.Web.Examples.ASP/Controllers/HomeController.cs index 759a080f..8eb7c806 100644 --- a/SpotifyAPI.Web.Examples.ASP/Controllers/HomeController.cs +++ b/SpotifyAPI.Web.Examples.ASP/Controllers/HomeController.cs @@ -3,25 +3,15 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using SpotifyAPI.Web.Examples.ASP.Models; namespace SpotifyAPI.Web.Examples.ASP.Controllers { + [Authorize(AuthenticationSchemes = "Spotify")] public class HomeController : Controller { - private readonly ILogger _logger; - - public HomeController(ILogger logger) - { - _logger = logger; - } - public async Task Index() { - if(!User.Identity.IsAuthenticated) - return Challenge(new AuthenticationProperties { RedirectUri = "/" }, "Spotify"); - var accessToken = await HttpContext.GetTokenAsync("Spotify", "access_token"); SpotifyWebAPI api = new SpotifyWebAPI { @@ -34,11 +24,6 @@ namespace SpotifyAPI.Web.Examples.ASP.Controllers return View(new IndexModel { SavedTracks = savedTracks }); } - public IActionResult Privacy() - { - return View(); - } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/SpotifyAPI.Web.Examples.ASP/README.md b/SpotifyAPI.Web.Examples.ASP/README.md new file mode 100644 index 00000000..70b59afe --- /dev/null +++ b/SpotifyAPI.Web.Examples.ASP/README.md @@ -0,0 +1,14 @@ +# ASP.NET Core Spotify Example + +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. + +## Important files/lines + +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: + +`dotnet user-secrets set client_secret YOUR_CLIENT_SECRET` +`dotnet user-secrets set client_id YOUR_CLIENT_ID` + +Also, it specifies the callback URL (`/callback`). This has to be registered in your Spotify App Settings (`http://localhost:5000/callback`) + +The actual request to Spotify and making sure that the user is authenticated happens in `Controllers/HomeController.cs`, which should be self explanatory. diff --git a/SpotifyAPI.Web.Examples.ASP/Startup.cs b/SpotifyAPI.Web.Examples.ASP/Startup.cs index 56618677..78d46c8e 100644 --- a/SpotifyAPI.Web.Examples.ASP/Startup.cs +++ b/SpotifyAPI.Web.Examples.ASP/Startup.cs @@ -23,11 +23,7 @@ namespace SpotifyAPI.Web.Examples.ASP services.AddControllersWithViews(); services.AddAuthentication(o => o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(options => - { - options.LoginPath = "/signin"; - options.LogoutPath = "/signout"; - }) + .AddCookie() .AddSpotify(options => { var scopes = Scope.UserLibraryRead; diff --git a/SpotifyAPI.Web.Examples.ASP/Views/Home/Privacy.cshtml b/SpotifyAPI.Web.Examples.ASP/Views/Home/Privacy.cshtml deleted file mode 100644 index af4fb195..00000000 --- a/SpotifyAPI.Web.Examples.ASP/Views/Home/Privacy.cshtml +++ /dev/null @@ -1,6 +0,0 @@ -@{ - ViewData["Title"] = "Privacy Policy"; -} -

@ViewData["Title"]

- -

Use this page to detail your site's privacy policy.

diff --git a/SpotifyAPI.Web.Examples.ASP/Views/Shared/_Layout.cshtml b/SpotifyAPI.Web.Examples.ASP/Views/Shared/_Layout.cshtml index 63a1efaf..12280cd4 100644 --- a/SpotifyAPI.Web.Examples.ASP/Views/Shared/_Layout.cshtml +++ b/SpotifyAPI.Web.Examples.ASP/Views/Shared/_Layout.cshtml @@ -16,16 +16,6 @@ aria-expanded="false" aria-label="Toggle navigation"> -