Advertisement
Guest User

Untitled

a guest
Jan 24th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public void Configure(IApplicationBuilder app) {
  2. app.UseJwtBearerAuthentication(options => {
  3. options.AutomaticAuthenticate = true;
  4. options.AutomaticChallenge = true;
  5. options.TokenValidationParameters.ValidateAudience = false;
  6. options.Authority = Configuration.Get<string>("OAuth:Authority");
  7. options.ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
  8. metadataAddress: options.Authority + ".well-known/openid-configuration",
  9. configRetriever: new OpenIdConnectConfigurationRetriever(),
  10. docRetriever: new HttpDocumentRetriever() { RequireHttps = false });
  11. });
  12.  
  13. app.UseOpenIdConnectServer(configuration => {
  14. configuration.Issuer = new Uri(Configuration.Get<string>("OpenId:Issuer"));
  15. configuration.AllowInsecureHttp = true;
  16. configuration.AuthorizationEndpointPath = PathString.Empty;
  17. configuration.AuthenticationScheme = OpenIdConnectServerDefaults.AuthenticationScheme;
  18. configuration.Provider = new AuthorizationProvider();
  19. });
  20. }
  21.  
  22. public class AuthorizationProvider : OpenIdConnectServerProvider {
  23. public override Task ValidateClientAuthentication(ValidateClientAuthenticationContext context) {
  24. context.Skipped();
  25.  
  26. return Task.FromResult<object>(null);
  27. }
  28.  
  29. public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context) {
  30. string username = context.UserName;
  31. string password = context.Password;
  32.  
  33. UserManager<ApplicationUser> userManager = context.HttpContext.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
  34. ApplicationUser user = userManager.FindByNameAsync(username).Result;
  35.  
  36. if (userManager.CheckPasswordAsync(user, password).Result) {
  37. ClaimsIdentity identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
  38. identity.AddClaim(ClaimTypes.Name, username, "token id_token");
  39.  
  40. List<string> roles = userManager.GetRolesAsync(user).Result.ToList();
  41. foreach (string role in roles) {
  42. identity.AddClaim(ClaimTypes.Role, role, "token id_token");
  43. }
  44.  
  45. ClaimsPrincipal principal = new ClaimsPrincipal(identity);
  46. context.Validated(principal);
  47. } else {
  48. context.Rejected("invalid credentials");
  49. }
  50.  
  51. return Task.FromResult<object>(null);
  52. }
  53. }
  54.  
  55. $http({
  56. method: 'POST',
  57. url: 'connect/token',
  58. headers: {
  59. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  60. },
  61. data: $.param({
  62. grant_type: 'password',
  63. username: email,
  64. password: password
  65. })
  66. }).then(function (response) {
  67. if (response.status == 200) {
  68. var token = response.data.access_token;
  69. localStorage.setItem('token', token);
  70. }
  71. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement