コード例 #1
0
        private async Task <bool> UnlinkAccountForUser(string userId, string loginProvider, string providerKey)
        {
            string ownerAccount = await Logins.GetUserId(loginProvider, providerKey);

            if (ownerAccount == userId)
            {
                if ((await Logins.GetLogins(userId)).Count > 1)
                {
                    await Logins.Remove(userId, loginProvider, providerKey);

                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        public async Task <ActionResult> ExternalLoginCallback(string loginProvider, string returnUrl)
        {
            // Get the information about the user from the external login provider
            ClaimsIdentity id = await HttpContext.GetExternalIdentity();

            if (id == null)
            {
                return(View("ExternalLoginFailure"));
            }

            // Make sure the external identity is from the loginProvider we expect
            Claim providerKeyClaim = id.FindFirst(ClaimTypes.NameIdentifier);

            if (providerKeyClaim == null || providerKeyClaim.Issuer != loginProvider)
            {
                return(View("ExternalLoginFailure"));
            }

            // Succeeded so we should be able to lookup the local user name and sign them in
            string providerKey = providerKeyClaim.Value;
            string userId      = await Logins.GetUserId(loginProvider, providerKey);

            if (!String.IsNullOrEmpty(userId))
            {
                await SignIn(userId, id.Claims, isPersistent : false);
            }
            else
            {
                // No local user for this account
                if (User.Identity.IsAuthenticated)
                {
                    // If the current user is logged in, just add the new account
                    await Logins.Add(new UserLogin(User.Identity.GetUserId(), loginProvider, providerKey));
                }
                else
                {
                    ViewBag.ReturnUrl = returnUrl;
                    return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                        UserName = id.Name, LoginProvider = loginProvider
                    }));
                }
            }

            return(RedirectToLocal(returnUrl));
        }
コード例 #3
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Validate the user password
                if (await Secrets.Validate(model.UserName, model.Password))
                {
                    string userId = await Logins.GetUserId(IdentityConfig.LocalLoginProvider, model.UserName);
                    await SignIn(userId, model.RememberMe);

                    return(RedirectToLocal(returnUrl));
                }
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError(String.Empty, "The user name or password provided is incorrect.");
            return(View(model));
        }