Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var jwtOption = new AlpsJwtOption();

            jwtOption.SecurityKey = Configuration["JwtOption:SecurityKey"];
            jwtOption.Audience    = Configuration["JwtOption:Audience"];
            jwtOption.Issuer      = Configuration["JwtOption:Issuer"];
            services.AddSingleton(jwtOption);
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                /* options.Events = new JwtBearerEvents
                 * {
                 *   OnTokenValidated = ct =>
                 *   {
                 *       //Console.WriteLine(ct.HttpContext.Request.Path.Value);
                 *       var auth=ct.HttpContext.RequestServices.GetRequiredService<AlpsAuthorizationFilter>();
                 *       auth.
                 *
                 *       return Task.CompletedTask;
                 *   }
                 * };*/
                options.SaveToken = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,                                                            //是否验证Issuer
                    ValidateAudience         = true,                                                            //是否验证Audience
                    ValidateLifetime         = true,                                                            //是否验证失效时间
                    ValidateIssuerSigningKey = true,                                                            //是否验证SecurityKey
                    ValidAudience            = jwtOption.Audience,                                              //Audience
                    ValidIssuer      = jwtOption.Issuer,                                                        //Issuer,这两项和前面签发jwt的设置一致
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOption.SecurityKey)), //拿到SecurityKey
                    NameClaimType    = "idName"
                };
            });
            services.AddControllers();//.AddJsonOptions();//o => o.Filters.Add(typeof(AlpsAuthorizationFilter)));
            //services.AddMvc(o=>{o.Filters.Add(typeof(AlpsAuthorizationFilter));}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            services.AddDbContext <AlpsContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("AlpsContext"), b => b.MigrationsAssembly("Alps.Web.Service"));
            });
            //services.AddSingleton<AlpsAuthorizationFilter>();
            services.AddScoped <Alps.Domain.Service.StockService>();

            services.AddSpaStaticFiles(
                Configuration => { Configuration.RootPath = "wwwroot"; }
                );
            services.AddHttpsRedirection(o => o.HttpsPort = 443);
            ConfigModelInvalid(services);
            //services.AddCors();
        }
Beispiel #2
0
 public AuthController(AlpsContext context, AlpsJwtOption jwtOption)
 {
     this._context   = context;
     this._jwtOption = jwtOption;
 }