コード例 #1
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
                //remove dependency on SecurityBasicsContext and pass this task off to the service
                using (WbyContext db = new WbyContext())
                {
                    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
                        UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        //add extra information that we got back from oAuth login
                        db.ExternalUsers.Add(new ExtraUserInformation
                        {
                            UserId = newUser.Id,
                            FullName = model.FullName,
                            Link = model.Link
                        });

                        //Save the extra information
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

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

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
コード例 #2
0
ファイル: UserRepo.cs プロジェクト: jesseeuton/webraceyou
        public void Save(UserProfile user)
        {
            using (WbyContext context = new WbyContext())
            {
                if (user.Id < 0)
                {
                    context.UserProfiles.Add(user);
                }
                else
                {
                    UserProfile profile = context.UserProfiles.First(p => p.Id == user.Id);
                    profile.Preferences = user.Preferences;
                    profile.Substances  = user.Substances;
                    profile.UserName    = user.UserName;
                    profile.History     = user.History;
                }

                context.SaveChanges();
            }
        }