Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Startup
- {
- public static void Configuration(IAppBuilder app)
- {
- app.UseOAuthBearerAuthentication(
- new OAuthBearerAuthenticationOptions());
- app.UseOAuthAuthorizationServer(
- new OAuthAuthorizationServerOptions
- {
- TokenEndpointPath = new PathString("/Token"),
- Provider = new OAuthAuthorizationServerProvider()
- {
- OnValidateClientAuthentication = async c =>
- {
- c.Validated();
- },
- OnGrantResourceOwnerCredentials = async c =>
- {
- if (c.UserName == "alice" && c.Password == "supersecret")
- {
- Claim claim1 = new Claim(ClaimTypes.Name, c.UserName);
- Claim[] claims = new Claim[] { claim1 };
- ClaimsIdentity claimsIdentity =
- new ClaimsIdentity(
- claims, OAuthDefaults.AuthenticationType);
- c.Validated(claimsIdentity);
- }
- }
- },
- AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(40),
- AllowInsecureHttp = true,
- RefreshTokenProvider = new ApplicationRefreshTokenProvider()
- });
- }
- }
- public class ApplicationRefreshTokenProvider : AuthenticationTokenProvider
- {
- public override void Create(AuthenticationTokenCreateContext context)
- {
- // Expiration time in seconds
- int expire = 2 * 60;
- context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
- context.SetToken(context.SerializeTicket());
- }
- public override void Receive(AuthenticationTokenReceiveContext context)
- {
- context.DeserializeTicket(context.Token);
- }
- }
- public class Startup
- {
- public static void Configuration(IAppBuilder app)
- {
- app.UseOAuthBearerAuthentication(
- new OAuthBearerAuthenticationOptions());
- app.UseOAuthAuthorizationServer(
- new OAuthAuthorizationServerOptions
- {
- TokenEndpointPath = new PathString("/Token"),
- Provider = new OAuthAuthorizationServerProvider()
- {
- OnValidateClientAuthentication = async c =>
- {
- c.Validated();
- },
- OnGrantResourceOwnerCredentials = async c =>
- {
- //Add a string with the current date
- string dateNow = DateTime.UtcNow.ToString();
- if (c.UserName == "alice" && c.Password == "supersecret")
- {
- Claim claim1 = new Claim(ClaimTypes.Name, c.UserName);
- Claim[] claims = new Claim[] { claim1 };
- ClaimsIdentity claimsIdentity =
- new ClaimsIdentity(
- claims, OAuthDefaults.AuthenticationType);
- //Add a claim with the creationdate of the token
- claimsIdentity.AddClaim(new Claim("ceationDate", dateNow));
- c.Validated(claimsIdentity);
- }
- }
- },
- AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(40),
- AllowInsecureHttp = true,
- RefreshTokenProvider = new ApplicationRefreshTokenProvider()
- });
- }
- }
- public class ApplicationRefreshTokenProvider : AuthenticationTokenProvider
- {
- public override void Create(AuthenticationTokenCreateContext context)
- {
- //Get the claim which holds creation date
- DateTime creationDate = Convert.ToDateTime(clientid.Claims.Where(c => c.Type == "ceationDate").Single().Value);
- //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)
- DateTime now = DateTime.UtcNow.AddSeconds(-30);
- //If the time has passed more than 30 seconds from the time you got your original access and refresh token by providing credentials
- //you may not create and return new refresh tokens(Obviously the 30 seconds could be changed to something less or more aswell)
- if(now < ceationDate)
- {
- // Expiration time in seconds
- int expire = 2 * 60;
- context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
- context.SetToken(context.SerializeTicket());
- }
- }
- public override void Receive(AuthenticationTokenReceiveContext context)
- {
- context.DeserializeTicket(context.Token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement