public void ResponseSignIn(FormsResponseSignInContext context) { var authResult = new AuthenticationResult { Success = true }; ChatUser loggedInUser = GetLoggedInUser(context); var principal = new ClaimsPrincipal(context.Identity); // Do nothing if it's authenticated if (principal.IsAuthenticated()) { EnsurePersistentCookie(context); return; } ChatUser user = _repository.GetUser(principal); authResult.ProviderName = principal.GetIdentityProvider(); // The user exists so add the claim if (user != null) { if (loggedInUser != null && user != loggedInUser) { // Set an error message authResult.Message = String.Format("This {0} account has already been linked to another user.", authResult.ProviderName); authResult.Success = false; // Keep the old user logged in context.Identity.AddClaim(new Claim(JabbRClaimTypes.Identifier, loggedInUser.Id)); } else { // Login this user AddClaim(context, user); } } else if (principal.HasRequiredClaims()) { ChatUser targetUser = null; // The user doesn't exist but the claims to create the user do exist if (loggedInUser == null) { // New user so add them user = _membershipService.AddUser(principal); targetUser = user; } else { // If the user is logged in then link _membershipService.LinkIdentity(loggedInUser, principal); _repository.CommitChanges(); authResult.Message = String.Format("Successfully linked {0} account.", authResult.ProviderName); targetUser = loggedInUser; } AddClaim(context, targetUser); } else if(!principal.HasPartialIdentity()) { // A partial identity means the user needs to add more claims to login context.Identity.AddClaim(new Claim(JabbRClaimTypes.PartialIdentity, "true")); } var cookieOptions = new CookieOptions { HttpOnly = true }; context.Response.Cookies.Append(Constants.AuthResultCookie, JsonConvert.SerializeObject(authResult), cookieOptions); }