public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return(this.RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { UserProfile userProfile = this.userManager.GetUserProfileFromProvider(provider, providerUserId); if (userProfile == null) { ClientUserData clientUserData = (ClientUserData)TempData["ClientUserData"]; Random random = new Random(); int userId = random.Next(int.MaxValue); userProfile = new UserProfile { RowKey = userId.ToString(), UserName = string.IsNullOrEmpty(clientUserData.UserName) ? clientUserData.Emails.Account : clientUserData.UserName, AccountEmail = clientUserData.Emails.Account, FirstName = model.FirstName, LastName = model.LastName, City = model.City, Country = model.Country, State = model.State, PreferredEmail = model.PreferredEmail, Gender = clientUserData.Gender, Link = Convert.ToString(clientUserData.Link), IsSuperAdmin = false, TimeZone = model.TimeZone, CreatedTime = DateTime.Now.ToUniversalTime().ToString(), Address1 = model.Address1, Address2 = model.Address2, Phone = model.Phone, PhoneCountryCode = model.PhoneCountryCode, ZipCode = model.ZipCode }; var regions = this.regionSource.GetAvailableRegions(); List <AccessDetails> defaultAccess = new List <AccessDetails>(); foreach (var region in regions) { var accessDetail = new AccessDetails { AccessLevel = AccessLevels.PortalUser, Authority = (Authorities)Convert.ToInt32(region.RegionInformation.Id) }; defaultAccess.Add(accessDetail); } this.userManager.SaveUserDetails( new UserDetails { UserInfo = userProfile, AccessInfo = defaultAccess, }); this.RegisterAuditor.TransactionId = this.RegisterLogger.TransactionId; this.RegisterAuditor.Audit(AuditId.RegisterUser, AuditStatus.Success, default(int), userProfile.UserName + " Registered Successfully"); this.RegisterLogger.Log(TraceEventType.Information, LoggingMessageId.PortalUserRegistration, userProfile.UserName + " registered to the portal"); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); this.RegisterLogger.Log(TraceEventType.Information, LoggingMessageId.PortalUserLoggedIn, userProfile.UserName + " has logged in to the portal"); // By Default IsSuperAdmin is false this.CreateAuthenticationTicket(model.UserName, clientUserData.AccessToken, DateTime.Now, false, false); return(this.RedirectToLocal(returnUrl)); } else { ModelState.AddModelError("UserName", "User already exists. Please enter a different user name."); } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; ViewBag.Country = new SelectList(Utility.GetCounties()); ViewBag.TimeZone = new SelectList(Utility.GetTimeZones()); ViewBag.PhoneCountryCode = new SelectList(Utility.GetCountryPhoneCodes()); return(this.View("Register", model)); }
public ActionResult ExternalLoginCallback(string returnUrl) { AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(returnUrl); if (!result.IsSuccessful) { return(this.RedirectToAction("ExternalLoginFailure")); } if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false)) { UserProfile userProfile = this.userManager.GetUserProfileFromProvider(result.Provider, result.ProviderUserId); IEnumerable <AccessDetails> accessDetails = this.userManager.GetUserAccessDetails(userProfile.RowKey); var isRegionAdmin = accessDetails.Any(x => x.AccessLevel == AccessLevels.Admin); this.CreateAuthenticationTicket(result.ExtraData["name"].ToString(), result.ExtraData["accesstoken"].ToString(), DateTime.Now, userProfile.IsSuperAdmin, isRegionAdmin); this.RegisterLogger.Log(TraceEventType.Information, LoggingMessageId.PortalUserLoggedIn, userProfile.UserName + " has logged in to the portal"); return(this.RedirectToLocal(returnUrl)); //// return this.Redirect(redirectUrl); } if (User.Identity.IsAuthenticated) { // If the current user is logged in add the new account OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name); UserProfile userProfile = this.userManager.GetUserProfileFromProvider(result.Provider, result.ProviderUserId); IEnumerable <AccessDetails> accessDetails = this.userManager.GetUserAccessDetails(userProfile.RowKey); var isRegionAdmin = accessDetails.Any(x => x.AccessLevel == AccessLevels.Admin); this.CreateAuthenticationTicket(User.Identity.Name, result.ExtraData["accesstoken"].ToString(), DateTime.Now, userProfile.IsSuperAdmin, isRegionAdmin); return(this.RedirectToLocal(returnUrl)); } else { // User is new, ask for them to register string accessToken = result.ExtraData["accesstoken"].ToString(); var userData = this.userManager.GetClientUserDataByAccessToken(accessToken); userData.AccessToken = accessToken; this.TempData["ClientUserData"] = userData; RegisterExternalLoginModel registerModel = new RegisterExternalLoginModel { FirstName = userData.FirstName, LastName = userData.LastName, UserName = string.IsNullOrEmpty(userData.UserName) ? userData.Emails.Account : userData.UserName, AccountEmail = userData.Emails.Account, PreferredEmail = userData.Emails.Preferred, City = userData.Business != null ? userData.Business.City : string.Empty, ExternalLoginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId), }; ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName; ViewBag.ReturnUrl = returnUrl; ViewBag.Country = new SelectList(Utility.GetCounties()); ViewBag.TimeZone = new SelectList(Utility.GetTimeZones(), "(UTC-08:00) Pacific Time (US & Canada)"); ViewBag.PhoneCountryCode = new SelectList(Utility.GetCountryPhoneCodes(), "United States(+1)"); return(this.View("Register", registerModel)); } }