Sky.Net/API/Extensions/IdentityServiceExtensions.cs

34 lines
1.3 KiB
C#
Raw Normal View History

2022-05-19 15:47:12 -07:00
using System.Text;
2022-05-19 13:50:10 -07:00
using Core.Entities.Identity;
using Infrastructure.Identity;
2022-05-19 15:47:12 -07:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2022-05-19 13:50:10 -07:00
using Microsoft.AspNetCore.Identity;
2022-05-19 15:47:12 -07:00
using Microsoft.IdentityModel.Tokens;
2022-05-19 13:50:10 -07:00
namespace API.Extensions
{
public static class IdentityServiceExtensions
{
2022-05-19 15:47:12 -07:00
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
2022-05-19 13:50:10 -07:00
{
var builder = services.AddIdentityCore<AppUser>();
builder = new IdentityBuilder(builder.UserType, builder.Services);
builder.AddEntityFrameworkStores<AppIdentityDbContext>();
builder.AddSignInManager<SignInManager<AppUser>>();
2022-05-19 15:47:12 -07:00
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Token:Key"])),
ValidIssuer = config["Token:Issuer"],
ValidateIssuer = true,
ValidateAudience = false
};
});
2022-05-19 13:50:10 -07:00
return services;
}
}
}