× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Multiple Token lifetimes in WebAPI2 with asp.net Identity

Charles King
Feb 03, 2015
<p>I was able to fix this my inserting the following into my OAuth provider (ApplicationOAuthProvider.cs) from the sample-</p> <pre><code> public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.Get&lt;TUserManager&gt;(); TUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } //if user, expire 60. If Admin, 14 days if (userManager.IsInRole(user.Id, "Users")) { context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromDays(60); } else { context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromDays(14); } ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties(user.UserName); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/23303117/Multiple%20Token%20lifetimes%20in%20WebAPI2%20with%20asp.net%20Identity/23303585">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus