Beispiel #1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login"));
            }

            // Save latest to database
            var dbUser = await UserManager.FindAsync(new UserLoginInfo(loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey));

            // Update to latest slack uesrname
            var slackUserName = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == ClaimTypes.Name).Select(c => c.Value).SingleOrDefault();

            if (!String.IsNullOrEmpty(slackUserName) && dbUser != null && !dbUser.UserName.Equals(slackUserName, StringComparison.OrdinalIgnoreCase))
            {
                dbUser.UserName = slackUserName;
                await UserManager.UpdateAsync(dbUser);
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false);

            switch (result)
            {
            case SignInStatus.Success:
                //TODO: update user name since it can change
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    UserName = loginInfo.DefaultUserName
                }));
            }
        }