コード例 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddCors(options =>
            {
                options.AddPolicy("AllowOrigin",
                                  builder =>
                {
                    builder.WithOrigins("http://localhost:3000");
                });
            });
            var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidIssuer              = tokenOptions.Issuer,
                    ValidAudience            = tokenOptions.Audience,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = SecuriytKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey)
                };
            });
        }
コード例 #2
0
        public AccessToken CreateToken(User user, List <OperationClaim> operationClaims)
        {
            var securityKey        = SecuriytKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey);
            var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey);
            var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims);
            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var token = jwtSecurityTokenHandler.WriteToken(jwt);

            return(new AccessToken
            {
                Token = token,
                Expiration = _accessTokenExpiration
            });
        }