mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-10-17 07:23:08 +01:00
Simpliefied Example and added README
This commit is contained in:
parent
58de9d9a4e
commit
2635a25301
@ -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()
|
||||
{
|
||||
|
14
SpotifyAPI.Web.Examples.ASP/README.md
Normal file
14
SpotifyAPI.Web.Examples.ASP/README.md
Normal 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.
|
@ -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;
|
||||
|
@ -1,6 +0,0 @@
|
||||
@{
|
||||
ViewData["Title"] = "Privacy Policy";
|
||||
}
|
||||
<h1>@ViewData["Title"]</h1>
|
||||
|
||||
<p>Use this page to detail your site's privacy policy.</p>
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user