Пример #1
0
 public EasyJwt(EasyJwtOption option)
 {
     _option = option;
 }
Пример #2
0
        public static IServiceCollection AddEasyJwt(this IServiceCollection services, EasyJwtOption option)
        {
            var easyJwt   = new EasyJwt(option);
            var jwtParams = easyJwt.ExportTokenParameters();

            services.AddDataProtection();
            services.AddSingleton(easyJwt);

            var authBuilder = services.AddAuthentication(authOptions =>
            {
                authOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
                              .AddJwtBearer(jwtOptions =>
            {
                jwtOptions.Audience     = option.Audience;
                jwtOptions.ClaimsIssuer = option.Issuer;
                jwtOptions.TokenValidationParameters = jwtParams;
                option.JwtOptions?.Invoke(jwtOptions);
            });

            if (option.EnableCookie)
            {
                authBuilder.AddCookie(options =>
                {
                    options.TicketDataFormat =
                        new EasyJwtAuthTicketFormat(jwtParams);
                    options.ClaimsIssuer     = option.Issuer;
                    options.LoginPath        = "/Login";
                    options.AccessDeniedPath = "/Login";
                    options.Cookie.HttpOnly  = true;
                    options.Cookie.Name      = "tk";
                    option.CookieOptions?.Invoke(options);
                });
            }

            return(services);
        }