There is a standard list of most common ClaimTypes types in the System.Security.Claims.ClaimTypes class and I suggest you add them as separate claims. After that, all other claims can be added as soon as UserData. I'm not sure why your application has up to 70 permissions? When you design your infrastructure, it is wise to use best practices that should use role-based authorization. I make it all easier. And remember that these are tried and tested methods. Windows Azure Active Directory has the same design, and yet it can handle any authentication and authorization scenario.
If you need more information on how you can use Json Web Tokens to implement such an authentication mechanism, see my article on this subject here .
Given Jwt, here is how you can check if a user has permissions.
private static ClaimsPrincipal ValidateToken(string token, string secret, bool checkExpiration) { var jsonSerializer = new JavaScriptSerializer(); var payloadJson = JsonWebToken.Decode(token, secret); var payloadData = jsonSerializer.Deserialize<Dictionary<string, object>>(payloadJson); object exp; if (payloadData != null && (checkExpiration && payloadData.TryGetValue("exp", out exp))) { var validTo = FromUnixTime(long.Parse(exp.ToString())); if (DateTime.Compare(validTo, DateTime.UtcNow) <= 0) { throw new Exception( string.Format("Token is expired. Expiration: '{0}'. Current: '{1}'", validTo, DateTime.UtcNow)); } } var subject = new ClaimsIdentity("Federation", ClaimTypes.Name, ClaimTypes.Role); var claims = new List<Claim>(); if (payloadData != null) foreach (var pair in payloadData) { var claimType = pair.Key; var source = pair.Value as ArrayList; if (source != null) { claims.AddRange(from object item in source select new Claim(claimType, item.ToString(), ClaimValueTypes.String)); continue; } switch (pair.Key) { case "name": claims.Add(new Claim(ClaimTypes.Name, pair.Value.ToString(), ClaimValueTypes.String)); break; case "surname": claims.Add(new Claim(ClaimTypes.Surname, pair.Value.ToString(), ClaimValueTypes.String)); break; case "email": claims.Add(new Claim(ClaimTypes.Email, pair.Value.ToString(), ClaimValueTypes.Email)); break; case "role": claims.Add(new Claim(ClaimTypes.Role, pair.Value.ToString(), ClaimValueTypes.String)); break; case "userId": claims.Add(new Claim(ClaimTypes.UserData, pair.Value.ToString(), ClaimValueTypes.Integer)); break; default: claims.Add(new Claim(claimType, pair.Value.ToString(), ClaimValueTypes.String)); break; } } subject.AddClaims(claims); return new ClaimsPrincipal(subject); }
I hope this helps
thanks
Stewart
source share