Exemple #1
0
 public bool TimeOut()
 {
     if (ExpiresTime.CompareTo(DateTime.UtcNow) < 0)
     {
         return(true);
     }
     return(false);
 }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConfigsSeivce(Configuration);
            //读取设置配置项
            JwtParameterConfiguration jwtParameterConfig = new JwtParameterConfiguration();

            Configuration.Bind("JwtParameters", jwtParameterConfig);
            services.AddSingleton(jwtParameterConfig);
            ExpiresTime expiresTime = new ExpiresTime();

            Configuration.Bind("ExpiresTime", expiresTime);
            services.AddSingleton(expiresTime);
            SecurityKeys securityKeys = new SecurityKeys();

            Configuration.Bind("SecurityKeys", securityKeys);
            services.AddSingleton(securityKeys);
            AppInfo appInfo = new AppInfo();

            Configuration.Bind("AppInfo", appInfo);
            services.AddSingleton(appInfo);

            //生成RsaSecurityKey用于JWT Token签名
            var rsaKeyBytes = Convert.FromBase64String(securityKeys.RSAKey);
            var rsaProvider = new RSACryptoServiceProvider();

            rsaProvider.ImportCspBlob(rsaKeyBytes);
            RSAParameters rsaParams      = rsaProvider.ExportParameters(true);
            var           rsaSecurityKey = new RsaSecurityKey(rsaParams);

            services.AddSingleton(rsaSecurityKey);

            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultSignInScheme       = CookieAuthenticationDefaults.AuthenticationScheme;
            }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, option =>
            {
                option.Authority                 = jwtParameterConfig.Issuer;
                option.RequireHttpsMetadata      = false;
                option.Audience                  = jwtParameterConfig.Audience;
                option.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = JwtClaimTypes.Name,
                    RoleClaimType = JwtClaimTypes.Role,
                };
                option.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        if (context.Request.Headers.TryGetValue("Authorization", out var tokenInfo))
                        {
                            context.Token = tokenInfo[0].Split(" ")[1];
                        }
                        else if (context.Request.Cookies.TryGetValue(jwtParameterConfig.CookieName, out var token))
                        {
                            context.Token = token;
                        }
                        return(Task.CompletedTask);
                    },
                    OnTokenValidated = context =>
                                       new TokenValidatedInvoker().Invoke(context),
                };
            });