Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ConfigureAccessToken(IAppBuilder app)
- {
- var optionsConfigurationToken = new OAuthAuthorizationServerOptions()
- {
- //Permitindo acesso ao endereço de fornecimento do token de acesso sem
- //precisar de HTTPS (AllowInsecureHttp).
- //Em produção o valor deve ser false.
- AllowInsecureHttp = true,
- //Configurando o endereço do fornecimento do token de acesso (TokenEndpointPath).
- TokenEndpointPath = new PathString("/token"),
- //Configurando por quanto tempo um token de acesso já forncedido valerá (AccessTokenExpireTimeSpan).
- AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
- //Como verificar usuário e senha para fornecer tokens de acesso? Precisamos configurar o Provider dos tokens
- Provider = new ProviderTokenAccess()
- };
- //Estas duas linhas ativam o fornecimento de tokens de acesso numa WebApi
- app.UseOAuthAuthorizationServer(optionsConfigurationToken);
- app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
- }
- public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
- {
- var user = Users()
- .FirstOrDefault(x => x.Name == context.UserName
- && x.Password == context.Password);
- if (user == null)
- {
- context.SetError("invalid_grant",
- "Usuário não encontrado ou a senha está incorreta.");
- return;
- }
- var identyUser = new ClaimsIdentity(context.Options.AuthenticationType);
- identyUser.AddClaim(new Claim("sub", context.UserName));
- identyUser.AddClaim(new Claim(ClaimTypes.Role, "user"));
- context.Validated(identyUser);
- }
- public static IEnumerable<User> Users()
- {
- return new List<User>
- {
- new User { Name = "Marcelo", Password = "admin" },
- new User { Name = "Joao", Password = "12345" },
- };
- }
Add Comment
Please, Sign In to add comment