示例#1
0
        public async Task <IActionResult> LinkLoginCallbackAsync(ExternalLoginsModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID 'user.Id'."));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync(Convert.ToString(user.Id));

            if (info == null)
            {
                throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'.");
            }

            var result = await _userManager.AddLoginAsync(user, info);

            if (!result.Succeeded)
            {
                model.StatusMessage = "The external login was not added. External logins can only be associated with one account.";
                return(RedirectToAction());
            }

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            model.StatusMessage = "The external login was added.";
            return(View());
        }
示例#2
0
        public async Task <IActionResult> LinkLoginAsync(ExternalLoginsModel model, string provider)
        {
            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            // Request a redirect to the external login provider to link a login for the current user
            var redirectUrl = Url.Action("ExternalLogins", values: new { model = model });
            var properties  = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User));

            return(new ChallengeResult(provider, properties));
        }
示例#3
0
        public async Task <IActionResult> ExternalLogins()
        {
            var model = new ExternalLoginsModel();
            var user  = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID 'user.Id'."));
            }

            model.CurrentLogins = await _userManager.GetLoginsAsync(user);

            model.OtherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
                                .Where(auth => model.CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
                                .ToList();
            model.ShowRemoveButton = user.PasswordHash != null || model.CurrentLogins.Count > 1;

            return(View(model));
        }
        public async Task <IActionResult> ExternalLogins()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(BadRequest($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var model = new ExternalLoginsModel {
                CurrentLogins = await _userManager.GetLoginsAsync(user)
            };

            model.OtherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
                                .Where(auth => model.CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
                                .ToList();
            model.ShowRemoveButton = await _userManager.HasPasswordAsync(user) || model.CurrentLogins.Count > 1;

            return(Ok(model));
        }
示例#5
0
        public async Task <IActionResult> ExternalLogins(string username)
        {
            var user = await GetUserAsync();

            if (!user.HasUserName(username))
            {
                return(NotFound());
            }

            var model = new ExternalLoginsModel {
                CurrentLogins = await UserManager.GetLoginsAsync(user)
            };

            model.OtherLogins = (await SignInManager.GetExternalAuthenticationSchemesAsync())
                                .Where(auth => model.CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
                                .ToList();
            model.ShowRemoveButton = user.HasPassword() || model.CurrentLogins.Count > 1;
            model.StatusMessage    = StatusMessage;

            return(View(model));
        }
        public async Task <IActionResult> ExternalLogins()
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{this.userManager.GetUserId(this.User)}'.");
            }

            var model = new ExternalLoginsModel {
                CurrentLogins = await this.userManager.GetLoginsAsync(user)
            };

            model.OtherLogins = (await this.signInManager.GetExternalAuthenticationSchemesAsync())
                                .Where(auth => model.CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
                                .ToList();
            model.ShowRemoveButton = await this.userManager.HasPasswordAsync(user) || model.CurrentLogins.Count > 1;

            model.StatusMessage = this.StatusMessage;

            return(this.View(model));
        }
示例#7
0
        public async Task <IActionResult> RemoveLoginAsync(ExternalLoginsModel model, string loginProvider, string providerKey)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID 'user.Id'."));
            }

            var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey);

            if (!result.Succeeded)
            {
                model.StatusMessage = "The external login was not removed.";
                return(RedirectToAction());
            }

            await _signInManager.RefreshSignInAsync(user);

            model.StatusMessage = "The external login was removed.";
            return(View());
        }