Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.UseTokenBasedAuthentication();
- public static class UserAuthenticationExtension
- {
- public static void UseTokenBasedAuthentication(this IAppBuilder appBuilder)
- {
- appBuilder.Use<TokenBasedAuthMiddleware>(new TokenBasedAuthenticationOptions());
- }
- }
- public class TokenBasedAuthMiddleware : AuthenticationMiddleware<TokenBasedAuthenticationOptions>
- {
- public TokenBasedAuthMiddleware(OwinMiddleware nextMiddleware, TokenBasedAuthenticationOptions authOptions)
- : base(nextMiddleware, authOptions)
- { }
- protected override AuthenticationHandler<TokenBasedAuthenticationOptions> CreateHandler()
- {
- return new TokenAuthenticationHandler();
- }
- }
- public class TokenBasedAuthenticationOptions : AuthenticationOptions
- {
- public TokenBasedAuthenticationOptions() : base("x-company-auth")
- { }
- }
- public class TokenAuthenticationHandler : AuthenticationHandler<TokenBasedAuthenticationOptions>
- {
- protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
- {
- var token = Authentication.GetTokenFromCookie();
- if (token == null)
- return null;
- var tokenType = Authentication.GetTokenTypeFromCookie();
- if (tokenType == null)
- return null;
- UserInfo ui = await RystadGlobal.authApi.GetUserData(token, tokenType);
- List<string> userRoles = await RystadGlobal.api.Authorization(tokenType, token);
- ui.roles = userRoles;
- AuthenticationProperties authProperties = new AuthenticationProperties();
- authProperties.IssuedUtc = DateTime.UtcNow;
- authProperties.ExpiresUtc = DateTime.UtcNow.AddDays(1);
- authProperties.AllowRefresh = true;
- authProperties.IsPersistent = true;
- IList<Claim> claimCollection = new List<Claim>
- {
- new Claim(ClaimTypes.Name, ui.name)
- , new Claim(ClaimTypes.Email, ui.email)
- };
- foreach (string role in ui.roles)
- {
- claimCollection.Add(new Claim(ClaimTypes.Role, role));
- }
- claimCollection.Add(new Claim(ClaimTypes.Role, ui.role));
- var claimsIdentity = new RystadIdentity(claimCollection, token, tokenType, ui);
- AuthenticationTicket ticket = new AuthenticationTicket(claimsIdentity, authProperties);
- return ticket;
- }
- }
Add Comment
Please, Sign In to add comment