Ejemplo n.º 1
0
 public JwtAuthenticationService(IOptions <JwtManagement> jwtManagementOptions)
 {
     _tokenManagement = jwtManagementOptions.Value;
 }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // Register the Swagger services  --NSwag
            services.AddSwaggerDocument(options =>
            {
                options.PostProcess = document =>
                {
                    document.Info.Version        = "v1";
                    document.Info.Title          = "APISwagger";
                    document.Info.Description    = "ASP.NET Core web API";
                    document.Info.TermsOfService = "None";
                    document.Info.Contact        = new NSwag.OpenApiContact
                    {
                        Name  = "JiChengLee",
                        Email = "*****@*****.**",
                        Url   = "https://www.cnblogs.com/jicheng/"
                    };
                    document.Info.License = new NSwag.OpenApiLicense
                    {
                        Name = "JiChengLee",
                        Url  = "https://www.cnblogs.com/jicheng/"
                    };
                };
            });

            services.Configure <JwtManagement>(Configuration.GetSection("jwtSettingConfig"));


            JwtManagement jwtManagement = Configuration.GetSection("jwtSettingConfig").Get <JwtManagement>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata = false;
                options.SaveToken            = true;
                //Token Validation Parameters
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    //获取或设置要使用的Microsoft.IdentityModel.Tokens.SecurityKey用于签名验证。
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtManagement.Secret)),
                    //获取或设置一个System.String,它表示将使用的有效发行者检查代币的发行者。
                    ValidIssuer = jwtManagement.Issuer,
                    //获取或设置一个字符串,该字符串表示将用于检查的有效受众反对令牌的观众。
                    ValidAudience    = jwtManagement.Audience,
                    ValidateIssuer   = false,
                    ValidateAudience = false,
                };

                services.AddSingleton <ILogger, ILogger <JwtAuthenticationController> >();
                services.AddSingleton <ILogger, ILogger <DemoController> >();
            });


            services.AddScoped <IJwtAuthenticateService, JwtAuthenticationService>();
        }