示例#1
0
 private RedirectToRouteResult ExternalLoginOrSingUp(String provider,String providerUserId, String username)
 {
     using (UsersContext db = new UsersContext())
     {
         String userName = OAuthWebSecurity.GetUserName(provider, providerUserId);
         UserProfile user = db.UserProfiles.FirstOrDefault(x => x.UserName.Equals(username));
         // Check if user already exists
         if (user == null)
         {
             // Insert name into the profile table
             var userProfile = db.UserProfiles.Add(new UserProfile { UserName = username });
             db.SaveChanges();
             _userService.AddUser(new User(userProfile.UserName, "", userProfile.UserId));
             OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, username);
             OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
         }
         else if (userName != null)
         {
             OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
         }
         else
         {
             string loginData = OAuthWebSecurity.SerializeProviderUserId(provider, providerUserId);
             return RedirectToAction("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = username, ExternalLoginData = loginData });
         }
     }
     return RedirectToAction("Index", "Home");
 }
示例#2
0
        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 RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        var userProfile = db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();
                        _userService.AddUser(new User(userProfile.UserName,"", userProfile.UserId));
                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = provider;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }