-
Notifications
You must be signed in to change notification settings - Fork 1
/
Startup.Auth.cs
52 lines (49 loc) · 1.93 KB
/
Startup.Auth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using ASPNetCoreAngular2Payments.SimpleTokenProvider;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
namespace ASPNetCoreAngular2Payments
{
public partial class Startup
{
private IApplicationBuilder _app;
private void ConfigureAuth(IApplicationBuilder app)
{
_app = app;
app.UseSimpleTokenProvider(new TokenProviderOptions
{
Path = "/api/jwt",
Audience = Configuration["AppConfiguration:SiteUrl"],
Issuer = Configuration["AppConfiguration:SiteUrl"],
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["SecretKey"])),
SecurityAlgorithms.HmacSha256),
IdentityResolver = GetIdentity
});
}
private async Task<ClaimsIdentity> GetIdentity(string email, string password)
{
using (var serviceScope = _app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var userManager = serviceScope.ServiceProvider.GetService<UserManager<IdentityUser>>();
var user = await userManager.FindByEmailAsync(email);
if (user == null)
{
return null;
}
var result = await userManager.CheckPasswordAsync(user, password);
return result ? new ClaimsIdentity(
new GenericIdentity(email, "Token"),
new[]
{
new Claim("user_name", user.UserName), new Claim("user_id", user.Id)
}) : null;
}
}
}
}