示例#1
0
        public async Task <UserLoginResult> TryExternalLogin(string providedEmail = "", bool?didAcceptTerms = null)
        {
            var          template    = new LoginResultTemplate();
            IUserContext userContext = null;
            var          email       = providedEmail;

            template.ExternalLoginInfo = await signInManager.GetExternalLoginInfoAsync();

            if (template.ExternalLoginInfo == null)
            {
                template.RejectReasons.Add("signInManager.GetExternalLoginInfoAsync returned null");
            }
            else
            {
                template.User = await userManager.FindByLoginAsync(template.ExternalLoginInfo.LoginProvider, template.ExternalLoginInfo.ProviderKey);

                if (template.User == null)
                {
                    if (string.IsNullOrWhiteSpace(email))
                    {
                        email = template.ExternalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email);
                    }

                    if (!string.IsNullOrWhiteSpace(email) && email.Contains("@"))
                    {
                        template.User = await userManager.FindByNameAsync(email);
                    }
                }

                if (template.User == null)
                {
                    template.User = await CreateUserFromExternalLogin(template.ExternalLoginInfo, email, didAcceptTerms);
                }
            }

            if (template.User != null)
            {
                await loginRulesProcessor.ProcessAccountLoginRules(template);
            }

            if (template.SignInResult == SignInResult.Failed && template.User != null && template.RejectReasons.Count == 0)
            {
                template.SignInResult = await signInManager.ExternalLoginSignInAsync(template.ExternalLoginInfo.LoginProvider, template.ExternalLoginInfo.ProviderKey, isPersistent : false);

                if (template.SignInResult.Succeeded)
                {
                    // TODO:
                    //update last login time
                }
            }

            if (template.User != null &&
                template.SignInResult != SignInResult.Success &&
                template.SignInResult != SignInResult.TwoFactorRequired)
            {
                //clear the external login
                await signInManager.SignOutAsync();
            }

            if (template.User != null)
            {
                userContext = new UserContext(template.User);
            }

            return(new UserLoginResult(
                       template.SignInResult,
                       template.RejectReasons,
                       userContext,
                       template.MustAcceptTerms,
                       template.NeedsAccountApproval,
                       template.NeedsEmailConfirmation,
                       template.EmailConfirmationToken,
                       template.NeedsPhoneConfirmation,
                       template.ExternalLoginInfo
                       ));
        }