Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. public class Startup
  2. {
  3. public static void Configuration(IAppBuilder app)
  4. {
  5. app.UseOAuthBearerAuthentication(
  6. new OAuthBearerAuthenticationOptions());
  7.  
  8. app.UseOAuthAuthorizationServer(
  9. new OAuthAuthorizationServerOptions
  10. {
  11. TokenEndpointPath = new PathString("/Token"),
  12. Provider = new OAuthAuthorizationServerProvider()
  13. {
  14. OnValidateClientAuthentication = async c =>
  15. {
  16. c.Validated();
  17. },
  18. OnGrantResourceOwnerCredentials = async c =>
  19. {
  20. if (c.UserName == "alice" && c.Password == "supersecret")
  21. {
  22. Claim claim1 = new Claim(ClaimTypes.Name, c.UserName);
  23. Claim[] claims = new Claim[] { claim1 };
  24. ClaimsIdentity claimsIdentity =
  25. new ClaimsIdentity(
  26. claims, OAuthDefaults.AuthenticationType);
  27. c.Validated(claimsIdentity);
  28. }
  29. }
  30. },
  31. AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(40),
  32. AllowInsecureHttp = true,
  33. RefreshTokenProvider = new ApplicationRefreshTokenProvider()
  34. });
  35. }
  36. }
  37.  
  38. public class ApplicationRefreshTokenProvider : AuthenticationTokenProvider
  39. {
  40. public override void Create(AuthenticationTokenCreateContext context)
  41. {
  42. // Expiration time in seconds
  43. int expire = 2 * 60;
  44. context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
  45. context.SetToken(context.SerializeTicket());
  46. }
  47.  
  48. public override void Receive(AuthenticationTokenReceiveContext context)
  49. {
  50. context.DeserializeTicket(context.Token);
  51. }
  52. }
  53.  
  54. public class Startup
  55. {
  56. public static void Configuration(IAppBuilder app)
  57. {
  58. app.UseOAuthBearerAuthentication(
  59. new OAuthBearerAuthenticationOptions());
  60.  
  61. app.UseOAuthAuthorizationServer(
  62. new OAuthAuthorizationServerOptions
  63. {
  64. TokenEndpointPath = new PathString("/Token"),
  65. Provider = new OAuthAuthorizationServerProvider()
  66. {
  67. OnValidateClientAuthentication = async c =>
  68. {
  69. c.Validated();
  70. },
  71. OnGrantResourceOwnerCredentials = async c =>
  72. {
  73. //Add a string with the current date
  74. string dateNow = DateTime.UtcNow.ToString();
  75.  
  76. if (c.UserName == "alice" && c.Password == "supersecret")
  77. {
  78. Claim claim1 = new Claim(ClaimTypes.Name, c.UserName);
  79. Claim[] claims = new Claim[] { claim1 };
  80. ClaimsIdentity claimsIdentity =
  81. new ClaimsIdentity(
  82. claims, OAuthDefaults.AuthenticationType);
  83.  
  84. //Add a claim with the creationdate of the token
  85. claimsIdentity.AddClaim(new Claim("ceationDate", dateNow));
  86.  
  87. c.Validated(claimsIdentity);
  88. }
  89. }
  90. },
  91. AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(40),
  92. AllowInsecureHttp = true,
  93. RefreshTokenProvider = new ApplicationRefreshTokenProvider()
  94. });
  95. }
  96. }
  97.  
  98. public class ApplicationRefreshTokenProvider : AuthenticationTokenProvider
  99. {
  100. public override void Create(AuthenticationTokenCreateContext context)
  101. {
  102. //Get the claim which holds creation date
  103. DateTime creationDate = Convert.ToDateTime(clientid.Claims.Where(c => c.Type == "ceationDate").Single().Value);
  104. //Create a variable holding current time minus 30 seconds(This is how long time you can create new refresh tokens by providing your original refresh token)
  105. DateTime now = DateTime.UtcNow.AddSeconds(-30);
  106.  
  107.  
  108. //If the time has passed more than 30 seconds from the time you got your original access and refresh token by providing credentials
  109. //you may not create and return new refresh tokens(Obviously the 30 seconds could be changed to something less or more aswell)
  110. if(now < ceationDate)
  111. {
  112. // Expiration time in seconds
  113. int expire = 2 * 60;
  114. context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
  115. context.SetToken(context.SerializeTicket());
  116. }
  117. }
  118.  
  119. public override void Receive(AuthenticationTokenReceiveContext context)
  120. {
  121. context.DeserializeTicket(context.Token);
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement