using Microsoft.IdentityModel.Claims; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Security.Principal; using System.Threading; using System.Web; public static class IIdentityExtensions { public static string GetUserId(this IIdentity identity) { string id = ""; var p = Thread.CurrentPrincipal as IClaimsPrincipal; if (p != null) id= getADFSValue(Microsoft.IdentityModel.Claims.ClaimTypes.NameIdentifier); else id= getDefaultValue(Microsoft.IdentityModel.Claims.ClaimTypes.NameIdentifier); if (id == "") id = Guid.Empty.ToString(); return id; } public static bool isSSO(this IIdentity identity) { string authType=Thread.CurrentPrincipal.Identity.AuthenticationType; if (authType.ToLower() != "applicationcookie") return true; return false; } private static string getADFSValue(string t) { string id = ""; // Cast the Thread.CurrentPrincipal IClaimsPrincipal principal = (IClaimsPrincipal)Thread.CurrentPrincipal; var i = (IClaimsIdentity)principal.Identity; // Access claims foreach (Microsoft.IdentityModel.Claims.Claim claim in i.Claims) { if (claim.ClaimType == t) { id = claim.Value; break; } } return id; } private static string getDefaultValue(string t) { string id = "";// Guid.Empty.ToString(); // Cast the Thread.CurrentPrincipal var principal = Thread.CurrentPrincipal; var i = principal.Identity as System.Security.Claims.ClaimsIdentity; // Access claims foreach (var claim in i.Claims) { if (claim.Type == t) { id = claim.Value; break; } } return id; } public static string GetUserName(this IIdentity identity) { var p = Thread.CurrentPrincipal as IClaimsPrincipal; string name = ""; if (p != null) name = getADFSValue(Microsoft.IdentityModel.Claims.ClaimTypes.Name); else name = getDefaultValue(Microsoft.IdentityModel.Claims.ClaimTypes.Name); if (name == null) name = ""; string[] splprm = { "\\" }; string[] names = name.Split(splprm, StringSplitOptions.None); if (names.Length >= 2) name = names[1]; return name; } public static string GetName(this IIdentity identity) { var p = Thread.CurrentPrincipal as IClaimsPrincipal; string name = ""; if (p != null) name= getADFSValue(Microsoft.IdentityModel.Claims.ClaimTypes.Name); else name = getDefaultValue(Microsoft.IdentityModel.Claims.ClaimTypes.Name); if (name == null) name = ""; string[] splprm={"\\"}; string[] names = name.Split(splprm, StringSplitOptions.None); if (names.Length >= 2) name = names[1]; return name; } public static string GetID(this IIdentity identity) { string id = ""; var p = Thread.CurrentPrincipal as IClaimsPrincipal; if (p != null) id= getADFSValue(Microsoft.IdentityModel.Claims.ClaimTypes.NameIdentifier); else id= getDefaultValue(Microsoft.IdentityModel.Claims.ClaimTypes.NameIdentifier); if (id == "") id = Guid.Empty.ToString(); return id; } public static string GetIsAuthenicated(this IIdentity identity) { string id = ""; var principal = Thread.CurrentPrincipal; var i = principal.Identity as System.Security.Claims.ClaimsIdentity; // Access claims foreach (var claim in i.Claims) { id +=" "+claim.Type+" "+ claim.Value +Environment.NewLine; } return id; } }