Skip to content

Commit 58de9d9

Browse files
committed
Refactor ASP.NET Example to use AspNet.Security.OAuth.Spotify
1 parent 9a6bd1b commit 58de9d9

18 files changed

Lines changed: 140 additions & 309 deletions

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

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

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

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Security.Claims;
1+
using System.Diagnostics;
62
using System.Threading.Tasks;
73
using Microsoft.AspNetCore.Authentication;
84
using Microsoft.AspNetCore.Authorization;
95
using Microsoft.AspNetCore.Mvc;
10-
using SpotifyAPI.Web.Auth;
11-
using SpotifyAPI.Web.Enums;
6+
using Microsoft.Extensions.Logging;
127
using SpotifyAPI.Web.Examples.ASP.Models;
138

149
namespace SpotifyAPI.Web.Examples.ASP.Controllers
1510
{
16-
[Authorize]
1711
public class HomeController : Controller
1812
{
19-
public async Task<ViewResult> Index()
20-
{
21-
var claimsIdent = User.Identity as ClaimsIdentity;
22-
23-
SpotifyWebAPI api = GetSpotifyApiFromUser(claimsIdent);
24-
if (api == null)
25-
return View(new HomeModel());
26-
27-
string userId = claimsIdent.GetSpecificClaim("user_id");
28-
var playlists = await api.GetUserPlaylistsAsync(userId);
29-
return View(new HomeModel
30-
{
31-
Playlists = playlists
32-
});
33-
}
13+
private readonly ILogger<HomeController> _logger;
3414

35-
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
36-
public IActionResult Error()
15+
public HomeController(ILogger<HomeController> logger)
3716
{
38-
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
17+
_logger = logger;
3918
}
4019

41-
private static SpotifyWebAPI GetSpotifyApiFromUser(ClaimsIdentity claimsIdent)
20+
public async Task<IActionResult> Index()
4221
{
43-
// TODO: Add expires_in logic
22+
if(!User.Identity.IsAuthenticated)
23+
return Challenge(new AuthenticationProperties { RedirectUri = "/" }, "Spotify");
4424

45-
string accessToken = claimsIdent.GetSpecificClaim("access_token");
46-
string tokenType = claimsIdent.GetSpecificClaim("token_type");
47-
return new SpotifyWebAPI
25+
var accessToken = await HttpContext.GetTokenAsync("Spotify", "access_token");
26+
SpotifyWebAPI api = new SpotifyWebAPI
4827
{
4928
AccessToken = accessToken,
50-
TokenType = tokenType
29+
TokenType = "Bearer"
5130
};
31+
32+
var savedTracks = await api.GetSavedTracksAsync(50);
33+
34+
return View(new IndexModel { SavedTracks = savedTracks });
35+
}
36+
37+
public IActionResult Privacy()
38+
{
39+
return View();
40+
}
41+
42+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
43+
public IActionResult Error()
44+
{
45+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
5246
}
5347
}
54-
}
48+
}

‎SpotifyAPI.Web.Examples.ASP/Models/ErrorViewModel.cs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ public class ErrorViewModel
77
public string RequestId { get; set; }
88

99
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10-
11-
public string Message { get; set; }
1210
}
13-
}
11+
}

‎SpotifyAPI.Web.Examples.ASP/Models/HomeModel.cs‎

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using SpotifyAPI.Web.Models;
3+
4+
namespace SpotifyAPI.Web.Examples.ASP.Models
5+
{
6+
public class IndexModel
7+
{
8+
public Paging<SavedTrack> SavedTracks;
9+
}
10+
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore;
75
using Microsoft.AspNetCore.Hosting;
86
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
98
using Microsoft.Extensions.Logging;
109

1110
namespace SpotifyAPI.Web.Examples.ASP
@@ -14,11 +13,14 @@ public class Program
1413
{
1514
public static void Main(string[] args)
1615
{
17-
CreateWebHostBuilder(args).Build().Run();
16+
CreateHostBuilder(args).Build().Run();
1817
}
1918

20-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21-
WebHost.CreateDefaultBuilder(args)
22-
.UseStartup<Startup>();
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
2325
}
24-
}
26+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:7731",
7-
"sslPort": 44310
6+
"applicationUrl": "http://localhost:55802",
7+
"sslPort": 44320
88
}
99
},
1010
"profiles": {
@@ -18,10 +18,10 @@
1818
"SpotifyAPI.Web.Examples.ASP": {
1919
"commandName": "Project",
2020
"launchBrowser": true,
21-
"applicationUrl": "https://localhost:5001;http://localhost:5000",
21+
"applicationUrl": "http://localhost:5000",
2222
"environmentVariables": {
2323
"ASPNETCORE_ENVIRONMENT": "Development"
2424
}
2525
}
2626
}
27-
}
27+
}

‎SpotifyAPI.Web.Examples.ASP/SpotifyAPI.Web.Examples.ASP.csproj‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
<Project Sdk="Microsoft.NET.Sdk.Web">
33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
5-
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6-
<UserSecretsId>52b27bb1-dc8f-4151-8ad6-b6efdb8edbb7</UserSecretsId>
5+
<UserSecretsId>201e6943-a5b3-473c-b1da-a579b36ca283</UserSecretsId>
76
</PropertyGroup>
87
<ItemGroup>
9-
<ProjectReference Include="..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
8+
<ProjectReference Include="..\SpotifyAPI.Web\SpotifyAPI.Web.csproj" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<PackageReference Include="AspNet.Security.OAuth.Spotify" Version="3.0.0" />
1012
</ItemGroup>
1113
</Project>

0 commit comments

Comments
 (0)