mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-03 19:17:05 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			47 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Text;
 | 
						|
using API.Constants;
 | 
						|
using API.Data;
 | 
						|
using API.Entities;
 | 
						|
using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
						|
using Microsoft.AspNetCore.Identity;
 | 
						|
using Microsoft.Extensions.Configuration;
 | 
						|
using Microsoft.Extensions.DependencyInjection;
 | 
						|
using Microsoft.IdentityModel.Tokens;
 | 
						|
 | 
						|
namespace API.Extensions
 | 
						|
{
 | 
						|
    public static class IdentityServiceExtensions
 | 
						|
    {
 | 
						|
        public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
 | 
						|
        {
 | 
						|
            services.AddIdentityCore<AppUser>(opt =>
 | 
						|
                {
 | 
						|
                    // Change password / signin requirements here
 | 
						|
                    opt.Password.RequireNonAlphanumeric = false;
 | 
						|
                })
 | 
						|
                .AddRoles<AppRole>()
 | 
						|
                .AddRoleManager<RoleManager<AppRole>>()
 | 
						|
                .AddSignInManager<SignInManager<AppUser>>()
 | 
						|
                .AddRoleValidator<RoleValidator<AppRole>>()
 | 
						|
                .AddEntityFrameworkStores<DataContext>();
 | 
						|
            
 | 
						|
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 | 
						|
                .AddJwtBearer(options =>
 | 
						|
                {
 | 
						|
                    options.TokenValidationParameters = new TokenValidationParameters()
 | 
						|
                    {
 | 
						|
                        ValidateIssuerSigningKey = true,
 | 
						|
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["TokenKey"])),
 | 
						|
                        ValidateIssuer = false,
 | 
						|
                        ValidateAudience = false
 | 
						|
                    };
 | 
						|
                });
 | 
						|
            services.AddAuthorization(opt =>
 | 
						|
            {
 | 
						|
                opt.AddPolicy("RequireAdminRole", policy => policy.RequireRole(PolicyConstants.AdminRole));
 | 
						|
            });
 | 
						|
            
 | 
						|
            return services;
 | 
						|
        }
 | 
						|
    }
 | 
						|
} |