Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Startup
- {
- public IConfigurationRoot Configuration { get; }
- public Startup(IHostingEnvironment env)
- {
- IConfigurationBuilder builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json", true, true)
- .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
- .AddEnvironmentVariables();
- //---
- Configuration = builder.Build();
- }
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- app.Use(async (context, next) =>
- {
- try
- {
- await next();
- }
- catch (Exception ex)
- {
- if (context.Response.HasStarted)
- {
- throw;
- }
- context.Response.StatusCode = 500;
- await context.Response.WriteAsync(ex.ToString());
- }
- });
- //---
- app.UseMvc();
- //---
- const string secretKey = "mysupersecret_secretkey!123";
- SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
- SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
- //---
- const string audience = "Audience";
- const string issuer = "Issuer";
- //---
- TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = signingKey,
- ValidateIssuer = false,
- ValidIssuer = issuer,
- ValidateAudience = true,
- ValidAudience = audience,
- ValidateLifetime = true,
- ClockSkew = TimeSpan.Zero,
- AuthenticationType = JwtBearerDefaults.AuthenticationScheme
- };
- //---
- app.UseJwtBearerAuthentication(new JwtBearerOptions
- {
- AutomaticAuthenticate = true,
- AutomaticChallenge = true,
- TokenValidationParameters = tokenValidationParameters,
- AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
- });
- //---
- app.UseSimpleTokenProvider(new TokenProviderOptions
- {
- Path = "/api/token",
- Audience = audience,
- Issuer = issuer,
- SigningCredentials = signingCredentials,
- Expiration = TimeSpan.FromDays(1),
- IdentityResolver = GetIdentity
- });
- }
- private Task<ClaimsIdentity> GetIdentity(string username, string password)
- {
- if (username == "TEST" && password == "TEST123")
- {
- return Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { }));
- }
- return Task.FromResult<ClaimsIdentity>(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement