public static void RegisterTokenAuthorizationOptions(this IServiceCollection services)
        {
            var key = new RsaSecurityKey(GetNewRSAKey());

            var tokenOptions = new TokenAuthOptions
            {
                Audience           = _audience,
                Issuer             = _issuer,
                SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
            };

            services.AddInstance(tokenOptions);
        }
        public static void RegisterBearerAuthentication(this IApplicationBuilder app, TokenAuthOptions tokenAuthOptions)
        {
            app.Use(next => async context =>
            {
                try
                {
                    await next(context);
                }
                catch (Exception e)
                {
                    if (context.Response.HasStarted)
                    {
                        throw e;
                    }
                    context.Response.StatusCode = 401;
                }
            });


            app.UseJwtBearerAuthentication(options =>
            {
                options.TokenValidationParameters.IssuerSigningKey  = tokenAuthOptions.SigningCredentials.Key;
                options.TokenValidationParameters.ValidAudience     = tokenAuthOptions.Audience;
                options.TokenValidationParameters.ValidIssuer       = tokenAuthOptions.Issuer;
                options.TokenValidationParameters.ValidateSignature = true;
                options.TokenValidationParameters.ValidateLifetime  = true;
                options.TokenValidationParameters.ClockSkew         = TimeSpan.Zero;
            });
        }
示例#3
0
 public OAuthService(TokenAuthOptions tokenAuthOptions)
 {
     _tokenAuthOptions = tokenAuthOptions;
 }