Beispiel #1
0
        public ActionResult Callback(string id)
        {
            var service = BaseOauth2Service.GetService(id);

            if (service != null)
            {
                try
                {
                    var redirectUrl = service.ValidateLogin(Request);
                    if (redirectUrl != null)
                    {
                        return(Redirect(redirectUrl));
                    }

                    // This is demo, so I am not handling saving of data into database
                    //
                    AuthCallbackResult respModel = null;
                    AuthExternalMode   authMode  = TempData["AuthExternalMode"] as AuthExternalMode? ?? AuthExternalMode.Default;
                    if (authMode == AuthExternalMode.AttachLogin)
                    {
                        // var userSession = GetUserSession();
                        // if (userSession == null)
                        //    throw new Exception("Initial attach call was probably coming from other domain / session");

                        // var login = BaseAttachToExistingLogin(userSession.UserId, service.UserData);
                        respModel = new AuthCallbackResult {
                            RedirectUrl = "/Accounts/AttachLoginProviders"
                        };
                    }
                    else
                    {
                        // respModel = InsertNewUserIntoDatabase(service);
                        respModel = new AuthCallbackResult {
                            RedirectUrl = "/AuthExternal/LoginSuccess"
                        };
                    }


                    return(View(respModel));
                }
                catch (Exception ex)
                {
                    throw ex;
                    //RedirectToAction("Error");
                }
            }
            else
            {
                return(RedirectToAction("LoginFail"));
            }
        }
Beispiel #2
0
        public ActionResult Login(string id)
        {
            string absoluteUri = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);
            var    service     = BaseOauth2Service.GetService(id, absoluteUri);

            if (service != null)
            {
                var url = service.BeginAuthentication();
                return(Redirect(url));
            }
            else
            {
                return(RedirectToAction("LoginFail"));
            }
        }
Beispiel #3
0
        public ActionResult Login(string id, AuthExternalMode?mode)
        {
            var service = BaseOauth2Service.GetService(id);

            if (service != null)
            {
                var url = service.BeginAuthentication();

                if (mode.HasValue)
                {
                    TempData["AuthExternalMode"] = mode;
                }

                return(Redirect(url));
            }
            else
            {
                return(RedirectToAction("LoginFail"));
            }
        }
Beispiel #4
0
        public ActionResult Callback(string id)
        {
            string absoluteUri = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);
            var    service     = BaseOauth2Service.GetService(id, absoluteUri);

            if (service != null)
            {
                BaseUserData userData    = null;
                var          redirectUrl = service.ValidateLogin(Request);

                switch (id)
                {
                case "Facebook":
                    userData = service.GetUserInfo <FacebookUserData>();
                    break;

                case "Google":
                    userData = service.GetUserInfo <GoogleUserData>();
                    break;

                case "Twitter":
                    userData = service.GetUserInfo <TwitterUserData>();
                    break;
                }

                if (redirectUrl != null)
                {
                    return(Redirect(redirectUrl));
                }

                return(RedirectToAction("LoginSuccess"));
            }
            else
            {
                return(RedirectToAction("LoginFail"));
            }
        }
        public ActionResult Callback(string id)
        {
            var service = BaseOauth2Service.GetService(id);

            if (service != null)
            {
                try
                {
                    var redirectUrl = service.ValidateLogin(Request);
                    if (redirectUrl != null)
                    {
                        return(Redirect(redirectUrl));
                    }
                    // Lấy thông tin người dùng đăng nhập qua ứng dụng xã hội
                    var userInfo = service.UserData;
                    var userName = "";
                    // TH người dùng đăng kí không có thông tin về email
                    if (string.IsNullOrEmpty(userInfo.Email))
                    {
                        userName = (new AnonymousBAL()).GenerateUserNameWithoutEmail();
                    }
                    else
                    {
                        userName = EmailHelper.GetUserNameOfEmail(userInfo.Email);
                    }

                    string provider = "";
                    if (userInfo.AuthService == Oauth2Login.Core.ExternalAuthServices.Facebook)
                    {
                        provider = "Facebook";
                    }
                    else if (userInfo.AuthService == Oauth2Login.Core.ExternalAuthServices.Google)
                    {
                        provider = "Google";
                    }

                    if (OAuthWebSecurity.Login(provider, userInfo.UserId, createPersistentCookie: false))
                    {
                        return(Redirect("/Home/Index"));
                    }

                    if (User.Identity.IsAuthenticated)
                    {
                        OAuthWebSecurity.CreateOrUpdateAccount(provider, userInfo.UserId, User.Identity.Name);
                        return(Redirect("/Home/Index"));
                    }
                    else
                    {
                        using (UsersContext db = new UsersContext())
                        {
                            var user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == userName);
                            // Check if user already exists
                            if (user == null)
                            {
                                // Insert name into the profile table
                                db.UserProfiles.Add(new UserProfile {
                                    UserName = userName
                                });
                                db.SaveChanges();

                                OAuthWebSecurity.CreateOrUpdateAccount(provider, service.UserData.UserId, userName);
                                OAuthWebSecurity.Login(provider, service.UserData.UserId, createPersistentCookie: false);

                                return(Redirect("/Home/Index"));
                            }
                            else
                            {
                                ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                                return(Redirect("/Home/Index"));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                    //RedirectToAction("Error");
                }
            }
            else
            {
                return(RedirectToAction("LoginFail"));
            }
        }