public static string CreateSecureToken(AuthorizedUserView authorizedUserView, string publicKeyPath) { var json = JsonConvert.SerializeObject(authorizedUserView); using (var rsa = ImportPublicKey(File.ReadAllText(publicKeyPath))) { var source = Encoding.UTF8.GetBytes(json); var encrypted = rsa.Encrypt(source, false); return(Convert.ToBase64String(encrypted)); } }
public static IPrincipal BuildPrincipal(AuthorizedUserView userView, string secureToken = null) { var identity = new ClaimsIdentity("custom"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userView.UserId.ToString())); identity.AddClaim(new Claim(ClaimTypes.Name, userView.Login)); identity.AddClaim(new Claim(ClaimTypes.Role, GetRole(userView))); identity.AddClaim(new Claim(EnterpriseClaims.FIRST_NAME, userView.FirstName)); identity.AddClaim(new Claim(EnterpriseClaims.LAST_NAME, userView.LastName)); if (!string.IsNullOrEmpty(secureToken)) { identity.AddClaim(new Claim(EnterpriseClaims.SECURE_TOKEN, secureToken)); } var principal = new ClaimsPrincipal(identity); return(principal); }
private static string GetRole(AuthorizedUserView user) { switch (user.Department) { case Departments.ADMIN: return(EnterpriseRoles.ADMIN); case Departments.COUNTING: return(EnterpriseRoles.ACCOUNTANT); case Departments.SALE: return(EnterpriseRoles.SELLER); case Departments.STORE: return(EnterpriseRoles.WAREHOUSEMAN); case Departments.SUPPLY: return(EnterpriseRoles.SUPPLIER); default: return(null); } }