/// <summary> /// 添加身份认证 /// </summary> /// <typeparam name="IdT">ID类型</typeparam> /// <typeparam name="UserT">用户类型</typeparam> /// <param name="services">服务收藏</param> /// <param name="options">选项配置</param> /// <returns>服务收藏</returns> public static IServiceCollection AddIdentityAuth <IdT, UserT>(this IServiceCollection services, Action <IdentityAuthOptions> options = null) where UserT : BasicUserInfo <IdT> { var config = new IdentityAuthOptions(); if (options != null) { options(config); } services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton <IIdentityAuthReader <IdT, UserT>, IdentityAuthClaimReader <IdT, UserT> >(); services.AddSingleton <IIdentityAuthContextReader <IdT, UserT>, IdentityAuthClaimReader <IdT, UserT> >(); services.AddSingleton <ISimpleFactory <HttpContext, CommonUseData>, CommonUseDataFactory <IdT, UserT> >(); var localOption = config.LocalAuth; switch (config.AuthType) { case IdentityAuthType.COOKIES: services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o => { if (!string.IsNullOrWhiteSpace(localOption.LoginPath)) { if (localOption.IsRedirectToLogin) { o.Events.OnRedirectToLogin = (context) => { return(Task.Run(() => { context.Response.Redirect(localOption.LoginPath); })); }; } else { o.LoginPath = new PathString(localOption.LoginPath); } } if (!string.IsNullOrWhiteSpace(localOption.LogoutPath)) { if (localOption.IsRedirectToLogout) { o.Events.OnRedirectToLogout = (context) => { return(Task.Run(() => { context.Response.Redirect(localOption.LogoutPath); })); }; } else { o.LogoutPath = new PathString(localOption.LogoutPath); } } }); services.AddSingleton <IIdentityAuth <IdT, UserT>, IdentityCookieAuth <IdT, UserT> >(); services.AddSingleton <IIdentityExit, IdentityCookieAuth <IdT, UserT> >(); break; case IdentityAuthType.JWT: if (config.Config == null) { throw new NullReferenceException("配置属性不能为null"); } services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = AUCUtility.CreateTokenValiParam(config); }); services.AddSingleton <IIdentityTokenAuth, IdentityJwtAuth <IdT, UserT> >(); break; case IdentityAuthType.JWT_COOKIE: services.AddSingleton <IHttpContextAuthToken, CookieTokenAuthHandler>(); services.AddAuthentication(options => { options.AddScheme <CookieTokenAuthHandler>("DefaultJwtCookie", "DefaultJwtCookie"); options.DefaultAuthenticateScheme = "DefaultJwtCookie"; options.DefaultChallengeScheme = "DefaultJwtCookie"; }); break; case IdentityAuthType.JWT_COOKIE_HEADER: services.AddSingleton <IHttpContextAuthToken, CookieHeaderTokenAuthHandler>(); services.AddAuthentication(options => { options.AddScheme <CookieHeaderTokenAuthHandler>("DefaultJwtCookieHeader", "DefaultJwtCookieHeader"); options.DefaultAuthenticateScheme = "DefaultJwtCookieHeader"; options.DefaultChallengeScheme = "DefaultJwtCookieHeader"; }); break; } return(services); }
/// <summary> /// 构造方法 /// </summary> public JwtTokenAuthHandlerBase() { valiParam = AUCUtility.CreateTokenValiParam(); }