示例#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
                using (EFDataBase db = new EFDataBase())
                {
                    User user = db.Users().FirstOrDefault(u => u.Email.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        //db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        //db.SaveChanges();



                        bool facebookVerified;


                        var     client   = new Facebook.FacebookClient(Session["facebooktoken"].ToString());
                        dynamic response = client.Get("me", new { fields = "id,verified" });
                        if (response.ContainsKey("verified"))
                        {
                            facebookVerified = response["verified"];
                        }
                        else
                        {
                            facebookVerified = false;
                        }
                        var code = Guid.NewGuid().ToString();

                        var newUser = new User();


                        newUser.ActivationDate = DateTime.Now;
                        newUser.Code           = code;
                        newUser.Email          = model.UserName;
                        newUser.UserName       = model.Name;
                        newUser.CreateDate     = DateTime.Now.ToString();
                        newUser.FbId           = response["id"];

                        db.UsersList.Add(newUser);

                        db.SaveChanges();

                        RoleService.AddUserToRole(model.UserName, "Influencer");

                        user = UserService.GetUserbyEmail(model.UserName);

                        if (user == null)
                        {
                            SetMessage("Error.", BootstrapAlertTypes.Danger);
                            return(Redirect("/Account/login"));
                        }


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


                        var claims = new List <Claim>();
                        claims.Add(new Claim(ClaimTypes.Name, model.UserName));
                        claims.Add(new Claim(ClaimTypes.Email, model.UserName));
                        var id = new ClaimsIdentity(claims,
                                                    DefaultAuthenticationTypes.ApplicationCookie);

                        var ctx = Request.GetOwinContext();
                        var authenticationManager = ctx.Authentication;
                        authenticationManager.SignIn(id);


                        SetAuthCookie(model.UserName);


                        SetEncryptedCookie(Configuration.UserCookie, new Dictionary <String, String>
                        {
                            { "Email", user.Email },
                            { "Code", user.Code }
                        });


                        if (RoleService.IsUserInRole(model.UserName, "Admin"))
                        {
                            return(Redirect("/Magazines"));
                        }

                        if (RoleService.IsUserInRole(model.UserName, "Influencer"))
                        {
                            return(Redirect("/Influencer"));
                        }

                        if (!String.IsNullOrEmpty(returnUrl))
                        {
                            return(Redirect(returnUrl));
                        }
                    }
                    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));
        }