Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Security.Claims;
  4. using System.Security.Principal;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.IdentityModel.Tokens;
  8. using Microsoft.Extensions.Options;
  9.  
  10. namespace TokenAuthentication
  11. {
  12. public partial class Startup
  13. {
  14.  
  15. private void ConfigureAuth(IApplicationBuilder app)
  16. {
  17.  
  18. var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("TokenAuthentication:SecretKey").Value));
  19.  
  20.  
  21. var tokenValidationParameters = new TokenValidationParameters
  22. {
  23. // The signing key must match!
  24. ValidateIssuerSigningKey = true,
  25. IssuerSigningKey = signingKey,
  26. // Validate the JWT Issuer (iss) claim
  27. ValidateIssuer = true,
  28. ValidIssuer = Configuration.GetSection("TokenAuthentication:Issuer").Value,
  29. // Validate the JWT Audience (aud) claim
  30. ValidateAudience = true,
  31. ValidAudience = Configuration.GetSection("TokenAuthentication:Audience").Value,
  32. // Validate the token expiry
  33. ValidateLifetime = true,
  34. // If you want to allow a certain amount of clock drift, set that here:
  35. ClockSkew = TimeSpan.Zero
  36. };
  37.  
  38. app.UseJwtBearerAuthentication(new JwtBearerOptions
  39. {
  40. AutomaticAuthenticate = true,
  41. AutomaticChallenge = true,
  42. TokenValidationParameters = tokenValidationParameters
  43. });
  44.  
  45.  
  46.  
  47. app.UseCookieAuthentication(new CookieAuthenticationOptions
  48. {
  49. AutomaticAuthenticate = true,
  50. AutomaticChallenge = true,
  51. AuthenticationScheme = "Cookie",
  52. CookieName = Configuration.GetSection("TokenAuthentication:CookieName").Value,
  53. TicketDataFormat = new CustomJwtDataFormat(
  54. SecurityAlgorithms.HmacSha256,
  55. tokenValidationParameters)
  56. });
  57.  
  58. var tokenProviderOptions = new TokenProviderOptions
  59. {
  60. Path = Configuration.GetSection("TokenAuthentication:TokenPath").Value,
  61. Audience = Configuration.GetSection("TokenAuthentication:Audience").Value,
  62. Issuer = Configuration.GetSection("TokenAuthentication:Issuer").Value,
  63. SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
  64. IdentityResolver = GetIdentity
  65. };
  66.  
  67. app.UseMiddleware<TokenProviderMiddleware>(Options.Create(tokenProviderOptions));
  68.  
  69.  
  70. }
  71.  
  72. private Task<ClaimsIdentity> GetIdentity(string username, string password)
  73. {
  74. // Don't do this in production, obviously!
  75. if (username == "TEST" && password == "TEST123")
  76. {
  77. return Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { }));
  78. }
  79.  
  80. // Credentials are invalid, or account doesn't exist
  81. return Task.FromResult<ClaimsIdentity>(null);
  82. }
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement