public UserInfo RegisterExternalLogin(RegisterExternalLoginModel model) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User is already authenticated")); } // Insert a new user into the database UserProfile user = UnitOfWork.UserProfileRepository.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table UnitOfWork.UserProfileRepository.Add(new UserProfile { UserName = model.UserName, Email = model.Email }); UnitOfWork.Commit(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); Roles.AddUsersToRole(new string[] { model.UserName }, Settings.Default.DefaultRole); IPrincipal principal = new GenericPrincipal(new GenericIdentity(model.UserName), null); Thread.CurrentPrincipal = principal; HttpContext.Current.User = principal; return new UserInfo() { IsAuthenticated = true, Roles = new List<string> { Settings.Default.DefaultRole }, UserName = model.UserName }; } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User name already exists. Please enter a different user name")); } }
public RegisterExternalLoginModel ExternalLoginConfirmation(string returnUrl, string username, string provideruserid, string provider) { RegisterExternalLoginModel model = new RegisterExternalLoginModel(); if (!User.Identity.IsAuthenticated) { // User is new, ask for their desired membership name string loginData = OAuthWebSecurity.SerializeProviderUserId(provider, provideruserid); model.UserName = username; model.Email = username; model.DisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; model.ReturnUrl = returnUrl; model.ExternalLoginData = loginData; } return model; }