public static void AttachIdentityToRequest(HttpApplication application) { var context = application.Context; var request = application.Request; var token = request.Cookies[FormsAuthentication.FormsCookieName]; var ticket = FormsAuthentication.Decrypt(token.Value); CustomIdentity customIdentity = new CustomIdentity(context.User.Identity.Name); const string replacementOfSeparator = "@'@"; string[] lineSeparator = new string[] { "'\r" }; string[] userClaims = ticket.UserData.Replace("''", replacementOfSeparator).Split(lineSeparator, StringSplitOptions.None); foreach (string x in userClaims) { string[] keyValuePairs = x.Split('\''); if (!String.IsNullOrEmpty(keyValuePairs[0])) { customIdentity.Claims.Add(keyValuePairs[0], x.Substring(keyValuePairs[0].Length+1).Replace(replacementOfSeparator, "'")); } } //Sync both web context user and current principal context.User = Thread.CurrentPrincipal = new CustomPrincipal(GetMappedCustomIdentity(customIdentity)); }
protected void Page_Load(object sender, EventArgs e) { string UserData = "email'*****@*****.**'\rdisplayName'Jiangtao Hu'\ruserProvidersUniqueId'1234ABC'\r"; CustomIdentity customIdentity = new CustomIdentity("ABC"); const string replacementOfSeparator = "@'@"; string[] lineSeparator = new string[] { "'\r" }; string[] userClaims = UserData.Replace("''", replacementOfSeparator).Split(lineSeparator, StringSplitOptions.None); foreach (string x in userClaims) { string[] keyValuePairs = x.Split('\''); if (!String.IsNullOrEmpty(keyValuePairs[0])) { customIdentity.Claims.Add(keyValuePairs[0], x.Substring(keyValuePairs[0].Length).Replace(replacementOfSeparator, "'")); } } Response.Write(customIdentity.UserProvidersUniqueId); }
/// <summary> /// Map external identity attributes to local /// </summary> /// <param name="identity"></param> private void MapIdentity(CustomIdentity identity) { //TODO: Add your identity mapping logic here //The identity mapping solve the user attributes/profile data exchange problem between external and local system }
public SampleCustomIdentity(CustomIdentity originalIdentity) : base(originalIdentity.Name) { MapIdentity(originalIdentity); }
/// <summary> /// Mapping external system custom identity to local system identity. /// </summary> /// <param name="customIdentity">The custom Identity from login system</param> /// <returns>return a local custom identity</returns> private static ICustomIdentity GetMappedCustomIdentity(CustomIdentity customIdentity) { //get local identity from cache, if not get from plugin through configuarion setting //TODO--go through the autofac DI could easly solve the mapping issue. return customIdentity; }
private void loginUser(XmlElement authInfo, HttpResponse response,string returnUrl) { // Get the user's unique identifier (this will ALWAYS be returned regardless of the login provider string userProvidersUniqueID = authInfo.GetElementsByTagName("identifier")[0].InnerText; //Save janrain claims properties in cookie for future access. // get a unique identity name froma janrain CustomIdentity customIdentity = new CustomIdentity(userProvidersUniqueID); customIdentity.UserProvidersUniqueId = userProvidersUniqueID; string[] authFields = new string[] { "identifier", "displayName","providerName","primaryKey","preferredUsername", "gender","birthday","utcOffset","email","verifiedEmail","url"}; foreach (var x in authFields) { // See if the user's display name is provided (not supplied by some providers XmlNodeList nodeList = authInfo.GetElementsByTagName(x); string value = null; if (nodeList != null && nodeList.Count > 0) { // Got a display name value = nodeList[0].InnerText; customIdentity.Claims.Add(x, value); } } //Set the authentication cookie and go back to the home page FormsAuthenticationExt.SetAuthCookie(customIdentity); //FormsAuthentication.SetAuthCookie(userProvidersUniqueID, false); response.Redirect(returnUrl); }