Example #1
0
 public TokenGenerator(TokenGenerateOption option, IAuthRepository authRepository, string connString, string privateKey)
 {
     _option        = option;
     _connString    = connString;
     AuthRepository = authRepository;
     _privateKey    = privateKey;
 }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            string secretKey  = "encrypt_the_validate_site_key";
            var    signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
            var    options    = new TokenGenerateOption
            {
                Path               = "/token",
                Audience           = "http://validateSite.woailibian.com",
                Issuer             = "http://thisSite.woailibian.com",
                SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                Expiration         = TimeSpan.FromMinutes(15),
            };
            var userValidate = app.ApplicationServices.GetService <IUserValidate>();
            // var userValidate = new UserValidate();

            var tokenGenerator = new TokenGenerator(options, userValidate);

            app.Map(options.Path, tokenGenerator.GenerateToken);

            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("This Service only use for authentication! ");
            });
        }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                // app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            // 请求错误提示配置
            app.UseErrorHandling();

            // 启用验证
            app.UseAuthentication();
            app.UseAuthorization();

            // 插入中间件以将生成的Swagger公开为JSON端点
            app.UseSwagger();

            // 如果您想公开交互式文档,可以选择插入Swagger-ui中间件,并指定用于支持它的Swagger JSON端点。
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Entity Generator Asp.Net Core WebApi V1");
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "default",
                    "{controller=Home}/{action=Index}/{id?}");
            });

            var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("encrypt_the_validate_site_key"));
            var options    = new TokenGenerateOption
            {
                Path               = "/token",
                Audience           = "ten",
                Issuer             = "谭武成",
                SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                Expiration         = TimeSpan.FromDays(7)
            };

            var userAuthRepository = app.ApplicationServices.GetService <IAuthRepository>();
            var keyPairs           = Configuration.GetSection("RsaKeyPair").GetChildren();
            var privateKey         = "";

            foreach (var item in keyPairs)
            {
                if (item.Key != "PrivateKey")
                {
                    continue;
                }
                privateKey = item.Value;
                break;
            }
            var tokenGenerator = new TokenGenerator(options, userAuthRepository, privateKey);

            app.Map(options.Path, tokenGenerator.GenerateTokenAsync);
        }
Example #4
0
 public TokenGenerator(TokenGenerateOption option, IUserValidate validator)
 {
     _Option       = option;
     UserValidator = validator;
 }