Brunno Obristo

Brunno Obristo

  • NA
  • 29
  • 3.6k

Asp net web api token authentication

Jul 24 2019 1:48 PM
I'm using token authentication in my api, but when i test it in postman, it gives me an error 404 not found
 
(STARTUP.AUTH)
  1. public partial class Startup  
  2. {  
  3. public static OAuthAuthorizationServerOptions OAuthOptions { getprivate set; }  
  4. public void Configuration(IAppBuilder app)  
  5. {  
  6. app.UseOAuthBearerTokens(OAuthOptions);  
  7. }  
  8. static Startup()  
  9. {  
  10. OAuthOptions = new OAuthAuthorizationServerOptions  
  11. {  
  12. AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),  
  13. AllowInsecureHttp = true,  
  14. TokenEndpointPath = new PathString("/token"),  
  15. Provider = new OAuthProvider()  
  16. };  
  17. }  
  18. }  
(OAUTH PROVIDER)
  1. public class OAuthProvider : OAuthAuthorizationServerProvider  
  2. {  
  3. public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)  
  4. {  
  5. return Task.Factory.StartNew(() =>  
  6. {  
  7. string username = context.UserName;  
  8. string password = context.Password;  
  9. Usuarios user = new Usuarios().Get(username, password);  
  10. if(user != null)  
  11. {  
  12. List<Claim> claims = new List<Claim>  
  13. {  
  14. new Claim(ClaimTypes.Name , user.EmailUsuario),  
  15. new Claim("UserId", user.IdUsuario.ToString())  
  16. };  
  17. ClaimsIdentity OAuthIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);  
  18. context.Validated(new Microsoft.Owin.Security.AuthenticationTicket(OAuthIdentity, new Microsoft.Owin.Security.AuthenticationProperties() { }));  
  19. }  
  20. else  
  21. {  
  22. context.SetError("erro""erro");  
  23. }  
  24. });  
  25. }  
  26. public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)  
  27. {  
  28. if(context.ClientId == null)  
  29. {  
  30. context.Validated();  
  31. }  
  32. return Task.FromResult<object>(null);  
  33. }  
  34. }  
I have a user class with a list, but i guess that the problem isn't there.

Answers (1)