private void LoginUser(AuthenticationToken openidToken) {
			bool persistentCookie = false;
			if (string.IsNullOrEmpty(this.Request.QueryString["ReturnUrl"])) {
				FormsAuthentication.SetAuthCookie(openidToken.ClaimedIdentifier, persistentCookie);
				if (!string.IsNullOrEmpty(this.topWindowUrl.Value)) {
					Uri topWindowUri = new Uri(this.topWindowUrl.Value);
					string returnUrl = HttpUtility.ParseQueryString(topWindowUri.Query)["ReturnUrl"];
					if (string.IsNullOrEmpty(returnUrl)) {
						if (string.Equals(topWindowUri.AbsolutePath, Utilities.ApplicationRoot.AbsolutePath + "login.aspx", StringComparison.OrdinalIgnoreCase)) {
							// this happens when the user navigates deliberately directly to login.aspx
							Response.Redirect("~/");
						} else {
							Response.Redirect(this.topWindowUrl.Value);
						}
					} else {
						Response.Redirect(returnUrl);
					}
				} else {
					// This happens for unsolicited assertions.
					Response.Redirect("~/");
				}
			} else {
				FormsAuthentication.RedirectFromLoginPage(openidToken.ClaimedIdentifier, persistentCookie);
			}
		}
Example #2
0
		private static AuthenticationToken ProcessUserLogin(string claimedIdentifier, string friendlyIdentifier, ClaimsResponse claims, Token samlToken, bool trustedEmail) {
			// Create an account for this user if we don't already have one.
			AuthenticationToken openidToken = Database.DataContext.AuthenticationTokens.FirstOrDefault(token => token.ClaimedIdentifier == claimedIdentifier);
			if (openidToken == null) {
				// this is a user we haven't seen before.
				User user = new User();
				openidToken = new AuthenticationToken {
					ClaimedIdentifier = claimedIdentifier,
					FriendlyIdentifier = friendlyIdentifier,
				};
				user.AuthenticationTokens.Add(openidToken);

				// Gather information about the user if it's available.
				if (claims != null) {
					if (!string.IsNullOrEmpty(claims.Email)) {
						user.EmailAddress = claims.Email;
						user.EmailAddressVerified = trustedEmail;
					}
					if (!string.IsNullOrEmpty(claims.FullName)) {
						if (claims.FullName.IndexOf(' ') > 0) {
							user.FirstName = claims.FullName.Substring(0, claims.FullName.IndexOf(' ')).Trim();
							user.LastName = claims.FullName.Substring(claims.FullName.IndexOf(' ')).Trim();
						} else {
							user.FirstName = claims.FullName;
						}
					}
				} else if (samlToken != null) {
					string email, givenName, surname;
					if (samlToken.Claims.TryGetValue(ClaimTypes.Email, out email)) {
						user.EmailAddress = email;
						user.EmailAddressVerified = trustedEmail;
					}
					if (samlToken.Claims.TryGetValue(ClaimTypes.GivenName, out givenName)) {
						user.FirstName = givenName;
					}
					if (samlToken.Claims.TryGetValue(ClaimTypes.Surname, out surname)) {
						user.LastName = surname;
					}
				}

				Database.DataContext.AddToUsers(user);
			} else {
				openidToken.UsageCount++;
				openidToken.LastUsedUtc = DateTime.UtcNow;
			}
			return openidToken;
		}
		private void AddIdentifier(string claimedId, string friendlyId) {
			// Check that this identifier isn't already tied to a user account.
			// We do this again here in case the LoggingIn event couldn't verify
			// and in case somehow the OP changed it anyway.
			var existingToken = Database.DataContext.AuthenticationTokens.FirstOrDefault(token => token.ClaimedIdentifier == claimedId);
			if (existingToken == null) {
				var token = new AuthenticationToken();
				token.ClaimedIdentifier = claimedId;
				token.FriendlyIdentifier = friendlyId;
				Database.LoggedInUser.AuthenticationTokens.Add(token);
				Database.DataContext.SaveChanges();
				this.Repeater1.DataBind();

				// Clear the box for the next entry
				this.openIdSelector.Identifier = null;
			} else {
				if (existingToken.User == Database.LoggedInUser) {
					this.alreadyLinkedLabel.Visible = true;
				} else {
					this.differentAccountLabel.Visible = true;
				}
			}
		}
Example #4
0
        private static AuthenticationToken ProcessUserLogin(string claimedIdentifier, string friendlyIdentifier, ClaimsResponse claims, Token samlToken, bool trustedEmail)
        {
            // Create an account for this user if we don't already have one.
            AuthenticationToken openidToken = Database.DataContext.AuthenticationTokens.FirstOrDefault(token => token.ClaimedIdentifier == claimedIdentifier);

            if (openidToken == null)
            {
                // this is a user we haven't seen before.
                User user = new User();
                openidToken = new AuthenticationToken {
                    ClaimedIdentifier  = claimedIdentifier,
                    FriendlyIdentifier = friendlyIdentifier,
                };
                user.AuthenticationTokens.Add(openidToken);

                // Gather information about the user if it's available.
                if (claims != null)
                {
                    if (!string.IsNullOrEmpty(claims.Email))
                    {
                        user.EmailAddress         = claims.Email;
                        user.EmailAddressVerified = trustedEmail;
                    }
                    if (!string.IsNullOrEmpty(claims.FullName))
                    {
                        if (claims.FullName.IndexOf(' ') > 0)
                        {
                            user.FirstName = claims.FullName.Substring(0, claims.FullName.IndexOf(' ')).Trim();
                            user.LastName  = claims.FullName.Substring(claims.FullName.IndexOf(' ')).Trim();
                        }
                        else
                        {
                            user.FirstName = claims.FullName;
                        }
                    }
                }
                else if (samlToken != null)
                {
                    string email, givenName, surname;
                    if (samlToken.Claims.TryGetValue(ClaimTypes.Email, out email))
                    {
                        user.EmailAddress         = email;
                        user.EmailAddressVerified = trustedEmail;
                    }
                    if (samlToken.Claims.TryGetValue(ClaimTypes.GivenName, out givenName))
                    {
                        user.FirstName = givenName;
                    }
                    if (samlToken.Claims.TryGetValue(ClaimTypes.Surname, out surname))
                    {
                        user.LastName = surname;
                    }
                }

                Database.DataContext.AddToUsers(user);
            }
            else
            {
                openidToken.UsageCount++;
                openidToken.LastUsedUtc = DateTime.UtcNow;
            }
            return(openidToken);
        }
Example #5
0
 /// <summary>
 /// Create a new AuthenticationToken object.
 /// </summary>
 /// <param name="claimedIdentifier">Initial value of the ClaimedIdentifier property.</param>
 /// <param name="createdOnUtc">Initial value of the CreatedOnUtc property.</param>
 /// <param name="lastUsedUtc">Initial value of the LastUsedUtc property.</param>
 /// <param name="usageCount">Initial value of the UsageCount property.</param>
 /// <param name="authenticationTokenId">Initial value of the AuthenticationTokenId property.</param>
 public static AuthenticationToken CreateAuthenticationToken(global::System.String claimedIdentifier, global::System.DateTime createdOnUtc, global::System.DateTime lastUsedUtc, global::System.Int32 usageCount, global::System.Int32 authenticationTokenId)
 {
     AuthenticationToken authenticationToken = new AuthenticationToken();
     authenticationToken.ClaimedIdentifier = claimedIdentifier;
     authenticationToken.CreatedOnUtc = createdOnUtc;
     authenticationToken.LastUsedUtc = lastUsedUtc;
     authenticationToken.UsageCount = usageCount;
     authenticationToken.AuthenticationTokenId = authenticationTokenId;
     return authenticationToken;
 }
Example #6
0
 /// <summary>
 /// Deprecated Method for adding a new object to the AuthenticationTokens EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAuthenticationTokens(AuthenticationToken authenticationToken)
 {
     base.AddObject("AuthenticationTokens", authenticationToken);
 }