void StartupAuth_ExternalLoginAuthenticated(object sender, ExternalLoginInfo e) { if (!e.IsAuthenticated) { HttpContext.Current.Response.Redirect("/Login"); return; } var user = User.FindByEmail(e.Email); var error = string.Empty; ; if (e.Email.IsNullOrEmpty()) { error = "no-email"; } else if (user == null) { error = "not-registered"; // TODO: If in your project you want to register user as well the uncomment this line and comment the above one // user = CreateUser(e, user); } else if (user.IsDeactivated) { error = "deactivated"; } if (error.HasValue()) { HttpContext.Current.Response.Redirect("~/login?ReturnUrl=/login&email={0}&provider={1}&error={2}".FormatWith(e.Email, e.Issuer, error)); } user.LogOn(); HttpContext.Current.Response.Redirect("~/"); }
private static User CreateUser(ExternalLoginInfo e, User user) { var nameWithSpaces = Regex.Replace(e.UserName, @"((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))", " $1").Trim(); var lastSpaceIndex = nameWithSpaces.LastIndexOf(' '); var firstName = nameWithSpaces.Substring(0, lastSpaceIndex); var lastName = nameWithSpaces.Substring(lastSpaceIndex); throw new NotImplementedException("Creating user is not implemented."); // EXAMPLE: //user = Database.Save(new MyUserType //{ // Email = e.Email, // FirstName = firstName, // LastName = lastName, // Password = new Guid().ToString(), // Salt = new Guid().ToString() //}); //return user; }
private static void RetrieveFirstAndLastNameFromExternalPrincipal(ExternalLoginInfo externalLoginInfo, out string firstName, out string lastName) { var name = externalLoginInfo.ExternalPrincipal.FindFirstValue(System.Security.Claims.ClaimTypes.Name); firstName = string.Empty; lastName = string.Empty; if (string.IsNullOrEmpty(name)) return; var array = name.Split(' '); firstName = array[0]; lastName = array[1]; }