private bool IsUserAuthorized(HttpActionContext actionContext) { try { string token = FetchFromHeader(actionContext); // fetch authorization token from header if (token != null && !String.IsNullOrWhiteSpace(token)) { AuthenticationModule auth = new AuthenticationModule(); JwtSecurityToken userPayloadToken = auth.ValidateToken(token); if (userPayloadToken != null) { JWTAuthenticationIdentity identity = AuthenticationModule.PopulateUserIdentity(userPayloadToken); if (this.role == null || identity.Roles.Contains(this.role)) { actionContext.ControllerContext.RequestContext.Principal = identity.GetPrincipal(); return(true); } } } } catch (Exception ex) { LogManager.GetLogger().Error(ex); } return(false); }
public void TokenValidation() { AuthenticationModule authentication = new AuthenticationModule(); string token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbi50ZXN0Iiwibm9tIjoiQWRtaW5pc3RyYXRldXIiLCJwcmVub20iOiJBZG1pbmlzdHJhdGV1ciIsIm1haWwiOiJhZG1pbi5yZXNlYXVAbW9udHBlbGxpZXItZXBzaS5mciIsImNsYXNzZSI6IkFkbWluaXN0cmF0aW9uIiwicm9sZXMiOiJST0xFX1NVUEVSX0FETUlOIiwiaWF0IjoxNTQ0Nzc3NzgzLCJleHAiOjE1NDQ3ODQ5ODN9.ebNHIHnaOtiCTPJmP2a0V7vhkrCZB0S5-wpN2fkzOKk"; JwtSecurityToken securityToken = authentication.ValidateToken(token); Assert.IsNotNull(securityToken); JWTAuthenticationIdentity identity = AuthenticationModule.PopulateUserIdentity(securityToken); Assert.IsTrue(identity.Name.Equals("admin.test")); Assert.IsTrue(identity.Nom.Equals("Administrateur")); Assert.IsTrue(identity.Mail.Equals("*****@*****.**")); Assert.IsTrue(1 == 1); }
public TokenResponse Validate(string jwtToken, string userName) { string message; var tokenUsername = AuthenticationModule.ValidateToken(jwtToken, out message); if (userName.Equals(tokenUsername)) { return(new TokenResponse { Status = "Success", Message = "User validated successfully." }); } return(new TokenResponse { Status = "Invalid", Message = message }); }