Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public class Customer
  2. {
  3. public int Id { get; set; }
  4. public string Username { get; set; }
  5. public string Password { get; set; }
  6. public string Role{ get; set; }
  7. }
  8.  
  9. public class MyAuthorizationServerProvider : OAuthAuthorizationServerProvider
  10. {
  11.  
  12. public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
  13. {
  14. context.Validated(); //
  15. }
  16.  
  17. public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  18. {
  19. var identity = new ClaimsIdentity(context.Options.AuthenticationType);
  20.  
  21.  
  22. if (context.UserName == "user" && context.Password == "pass")
  23. {
  24. identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
  25. identity.AddClaim(new Claim("username", "user"));
  26. identity.AddClaim(new Claim(ClaimTypes.Name, "Suresh Sha"));
  27. context.Validated(identity);
  28. }
  29. else
  30. {
  31. context.SetError("invalid_grant", "Provided username and password is incorrect");
  32. return;
  33. }
  34. }
  35. }
  36.  
  37. if (context.UserName == "user" && context.Password == "pass")
  38. {
  39. identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
  40. identity.AddClaim(new Claim("username", "user"));
  41. identity.AddClaim(new Claim(ClaimTypes.Name, "NAME"));
  42. context.Validated(identity);
  43. }
  44.  
  45. public partial class Startup
  46. {
  47. public void Configuration(IAppBuilder app)
  48. {
  49. //enable cors origin requests
  50. app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
  51.  
  52. var myProvider = new MyAuthorizationServerProvider();
  53. OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
  54. {
  55. AllowInsecureHttp = true,
  56. TokenEndpointPath = new PathString("/token"),
  57. AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
  58. Provider = myProvider
  59. };
  60. app.UseOAuthAuthorizationServer(options);
  61. app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
  62.  
  63. //my code
  64.  
  65.  
  66. HttpConfiguration config = new HttpConfiguration();
  67. WebApiConfig.Register(config);
  68.  
  69. //ConfigureAuth(app);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement