Ejemplo n.º 1
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            FacebookOAuth2Client.RewriteRequest(); // needs to go before every call to verify authentication
            GoogleOAuth2Client.RewriteRequest();
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                // the u becomes a route parameter
                SimpleMembershipProvider provider = (SimpleMembershipProvider) Membership.Provider;
                int id = provider.GetUserIdFromOAuth(result.Provider, result.ProviderUserId);

                return RedirectToAction("Index", "UserProfile", new { u = id });
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                var providerDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;

                SimpleMembershipProvider provider = (SimpleMembershipProvider) Membership.Provider;
                int id = provider.GetUserIdFromOAuth(result.Provider, result.ProviderUserId);
                User user = ServiceManager.Kernel.Get<IUserManager>().GetById(id);

                if (providerDisplayName == "Google")
                {
                    user.GoogleToken = result.ExtraData["accesstoken"];
                }
                else if (providerDisplayName == "Facebook")
                {
                    user.DisplayName = result.ExtraData["name"];
                    user.FacebookProfileLink = result.ExtraData["link"];
                    user.Gender = result.ExtraData["gender"];
                    string stAccess = result.ExtraData["accesstoken"];
                    string ltAccess = ServiceManager.Kernel.Get<IFacebookManager>().GetFacebookLongTermAccessCode(stAccess);
                    string locationJson;
                    user.FacebookToken = ltAccess;
                    user.FacebookUserId = result.ProviderUserId;
                    result.ExtraData.TryGetValue("location", out locationJson);
                    if (locationJson != null)
                    {
                        dynamic locationName = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(locationJson);
                        user.Location = locationName.name;
                    }
                    string fbProfilePic = ServiceManager.Kernel.Get<IFacebookManager>().GetProfilePictureUrl(user);
                    user.PhotoLink = fbProfilePic;
                }

                ServiceManager.Kernel.Get<IUserManager>().Save(user);
                return RedirectToLocal("/UserProfile?u=" + user.Id);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                ViewBag.ReturnUrl = returnUrl;
                RegisterExternalLoginModel loginModel;
                loginModel = new RegisterExternalLoginModel
                    {
                        UserName = result.UserName,
                        ExternalLoginData = loginData,
                    };
                if (ViewBag.ProviderDisplayName == "Facebook")
                {
                    loginModel.PersonName = result.ExtraData["name"];
                    loginModel.Link = result.ExtraData["link"];
                    loginModel.Gender = result.ExtraData["gender"];
                    string stAccess = result.ExtraData["accesstoken"];
                    string ltAccess = ServiceManager.Kernel.Get<IFacebookManager>().GetFacebookLongTermAccessCode(stAccess);
                    string locationJson;
                    loginModel.FacebookToken = ltAccess;
                    loginModel.FacebookUserId = result.ProviderUserId;
                    result.ExtraData.TryGetValue("location", out locationJson);
                    if (locationJson != null) {
                        dynamic locationName = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(locationJson);
                        loginModel.Location = locationName.name;
                    }
                }
                else if (ViewBag.ProviderDisplayName == "Twitter")
                {
                        loginModel.TwitterToken = result.ExtraData["accesstoken"];
                        loginModel.PersonName = result.UserName;
                        loginModel.TwitterUserName = result.UserName;

                }

                else if (ViewBag.ProviderDisplayName == "Google")
                {
                    loginModel.GoogleToken = result.ExtraData["accesstoken"];
                    loginModel.GoogleUserId = result.ProviderUserId;
                }
                else
                {
                    loginModel = new RegisterExternalLoginModel
                    {
                        UserName = result.UserName,
                        ExternalLoginData = loginData
                    };
                }
                return View("ExternalLoginConfirmation", loginModel);
            }
        }
Ejemplo n.º 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 (Db db = new Db())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        var userProfile = new UserProfile { UserName = model.UserName };
                        User userData = new User {
                            DisplayName = model.PersonName,
                            Gender = model.Gender,
                            FacebookProfileLink = model.PhotoLink,
                            FacebookProfile = userProfile,
                            FacebookToken = model.FacebookToken,
                            FacebookUserId = model.FacebookUserId,
                            UserName = model.UserName,
                            Location = model.Location,
                            GoogleUserId = model.GoogleUserId,
                            GoogleToken = model.GoogleToken,
                            TwitterToken = model.TwitterToken,
                            TwitterUserName = model.TwitterUserName,

                        };
                        if (userData.FacebookToken != null) {
                            string fbProfilePic = ServiceManager.Kernel.Get<IFacebookManager>().GetProfilePictureUrl(userData);
                            userData.PhotoLink = fbProfilePic;
                        }
                        SaveResult<User> saveResult = ServiceManager.Kernel.Get<IUserManager>().Save(userData);

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

                        return RedirectToLocal("/UserProfile?u=" + saveResult.Result.Id);
                    }
                    else
                    {/*
                        if (model.GoogleUserId != null && model.GoogleUserId != "")
                        {
                            user.GoogleUserId = model.GoogleUserId;
                            user.GoogleToken = model.GoogleToken;
                            SaveResult<User> saveResult = ServiceManager.Kernel.Get<IUserManager>().Save(user);
                        }*/
                        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);
        }