コード例 #1
0
        public AccessToken CreateToken(User user, List <OperationClaim> operationClaims)
        {
            _accessTokenExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration);
            var securityKey        = SecurirtyKeyHelper.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
            });
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //AOP
            //Autofac, Ninject,CastleWindsor,StructureMap,LightInject,DryInject
            //AOP
            //Postsharp
            services.AddControllers();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>();

            services.AddCors();
            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         = SecurirtyKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey)
                };
                services.AddDependencyResolvers(new ICoreModule[] {
                    new CoreModule()
                });
            });
            //services.AddSingleton<IProductService, ProducManager>();
            //services.AddSingleton<IProductDal, EfProductDal>();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "WebAPI", Version = "v1"
                });
            });
        }