Guest User

Untitled

a guest
Oct 4th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. private void ConfigureAccessToken(IAppBuilder app)
  2. {
  3. var optionsConfigurationToken = new OAuthAuthorizationServerOptions()
  4. {
  5. //Permitindo acesso ao endereço de fornecimento do token de acesso sem
  6. //precisar de HTTPS (AllowInsecureHttp).
  7. //Em produção o valor deve ser false.
  8. AllowInsecureHttp = true,
  9.  
  10. //Configurando o endereço do fornecimento do token de acesso (TokenEndpointPath).
  11. TokenEndpointPath = new PathString("/token"),
  12.  
  13. //Configurando por quanto tempo um token de acesso já forncedido valerá (AccessTokenExpireTimeSpan).
  14. AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
  15.  
  16. //Como verificar usuário e senha para fornecer tokens de acesso? Precisamos configurar o Provider dos tokens
  17. Provider = new ProviderTokenAccess()
  18. };
  19.  
  20. //Estas duas linhas ativam o fornecimento de tokens de acesso numa WebApi
  21. app.UseOAuthAuthorizationServer(optionsConfigurationToken);
  22. app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
  23. }
  24.  
  25. public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  26. {
  27.  
  28. var user = Users()
  29. .FirstOrDefault(x => x.Name == context.UserName
  30. && x.Password == context.Password);
  31.  
  32. if (user == null)
  33. {
  34. context.SetError("invalid_grant",
  35. "Usuário não encontrado ou a senha está incorreta.");
  36. return;
  37. }
  38.  
  39. var identyUser = new ClaimsIdentity(context.Options.AuthenticationType);
  40. identyUser.AddClaim(new Claim("sub", context.UserName));
  41. identyUser.AddClaim(new Claim(ClaimTypes.Role, "user"));
  42. context.Validated(identyUser);
  43. }
  44.  
  45. public static IEnumerable<User> Users()
  46. {
  47. return new List<User>
  48. {
  49. new User { Name = "Marcelo", Password = "admin" },
  50. new User { Name = "Joao", Password = "12345" },
  51.  
  52. };
  53. }
Add Comment
Please, Sign In to add comment