Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddRouting();
            services.AddTransient <IRepository, Repository>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = AuthOptions.ISSUER,
                    ValidateAudience         = true,
                    ValidAudience            = AuthOptions.AUDIENCE,
                    ValidateLifetime         = true,
                    IssuerSigningKey         = AuthOptions.CreateSymmSecurityKey(),
                    ValidateIssuerSigningKey = true
                };
            });
            services.AddAuthorization();
            //services.AddSpaStaticFiles(conf =>
            //{
            //    conf.RootPath = "ClientApp/dist";
            //});
        }
Пример #2
0
        public string CreateAuthToken(UserBaseModel user)
        {
            var now      = DateTime.UtcNow;
            var identity = new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimsIdentity.DefaultNameClaimType, user.Name)
            });
            var jwt = new JwtSecurityToken(AuthOptions.ISSUER,
                                           AuthOptions.AUDIENCE, identity.Claims, now, now.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME)),
                                           new SigningCredentials(AuthOptions.CreateSymmSecurityKey(), SecurityAlgorithms.HmacSha256));
            var res = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(res);
        }