Simpliefied Example and added README

This commit is contained in:
Jonas Dellinger 2019-11-19 13:23:34 +01:00
parent 58de9d9a4e
commit 2635a25301
5 changed files with 16 additions and 37 deletions

View File

@ -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<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public async Task<IActionResult> 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()
{

View File

@ -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.

View File

@ -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;

View File

@ -1,6 +0,0 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
<p>Use this page to detail your site's privacy policy.</p>

View File

@ -16,16 +16,6 @@
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>