public IEnumerable <ExternalLoginResult> GetExternalLogins(string returnUrl, bool generateState = false)
        {
            IEnumerable <AuthenticationDescription> descriptions = Authentication.GetExternalAuthenticationTypes();
            List <ExternalLoginResult> logins = new List <ExternalLoginResult>();

            string state;

            if (generateState)
            {
                const int strengthInBits = 256;
                state = RandomOAuthStateGenerator.Generate(strengthInBits);
            }
            else
            {
                state = null;
            }

            foreach (AuthenticationDescription description in descriptions)
            {
                ExternalLoginResult login = new ExternalLoginResult {
                    Name = description.Caption,
                    Url  = Url.Route("ExternalLogin", new {
                        provider      = description.AuthenticationType,
                        response_type = "token",
                        client_id     = Auth.PublicClientId,
                        redirect_uri  = new Uri(Request.RequestUri, returnUrl).AbsoluteUri,
                        state         = state
                    }),
                    State = state
                };
                logins.Add(login);
            }

            return(logins);
        }
Exemple #2
0
        public void Can_serialize_simple_example2()
        {
            const string expected = @"{""name"":""spot""}";
            var          dog      = new ExternalLoginResult {
                IdUser = 1, Sc = "au1skm2qhjbwhmu4z0qwcpiv"
            };

            var actual = JsonParser.Serialize(dog);

            Assert.AreEqual(expected, actual);
        }
Exemple #3
0
        public async Task <IActionResult> ExternalLoginCallback(string remoteError = null)
        {
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                await FillViewBagWithExternalLoginsAsync();

                return(View(nameof(Login)));
            }

            ExternalLoginResult externalLoginResult = null;

            try
            {
                externalLoginResult = await _accountsService.ExternalLoginAsync();

                if (externalLoginResult.SignInSucceeded || externalLoginResult.CreateUserSucceeded)
                {
                    return(RedirectToAction(IndexActionName, HomeControllerName));
                }

                if (externalLoginResult.IsLockedOut)
                {
                    return(RedirectToAction(nameof(Lockout)));
                }
            }
            catch (ArgumentException e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }

            await FillViewBagWithExternalLoginsAsync();

            foreach (var error in externalLoginResult.CreateUserErrors)
            {
                ModelState.AddModelError(string.Empty, error);
            }

            return(View(nameof(Login)));
        }
        public async Task <ExternalLoginResult> ExternalLoginAsync()
        {
            var loginInfo = await _signInManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                throw new ArgumentException(ErrorLoadingExternalLogin);
            }

            var signInResult = await _signInManager.ExternalLoginSignInAsync(loginInfo.LoginProvider, loginInfo.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            var externalLoginResult = new ExternalLoginResult {
                SignInSucceeded = signInResult.Succeeded, IsLockedOut = signInResult.IsLockedOut
            };

            var email = loginInfo.Principal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Email)?.Value;
            var user  = new IdentityUser {
                UserName = email, Email = email, EmailConfirmed = true
            };

            var createResult = await _userManager.CreateAsync(user);

            externalLoginResult.CreateUserSucceeded = createResult.Succeeded;
            if (createResult.Succeeded)
            {
                var addLoginResult = await _userManager.AddLoginAsync(user, loginInfo);

                if (addLoginResult.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false, loginInfo.LoginProvider);
                }
            }

            externalLoginResult.CreateUserErrors = createResult.Errors.Select(error => error.Description).ToList();

            return(externalLoginResult);
        }
        public ActionResult ExternalLogin(string provider, string returnUrl)
        {
            ExternalLoginResult result = new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));

            return(result);
        }
 public ActionResult ExternalLogin(string provider, string returnUrl)
 {
     ExternalLoginResult result = new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
     return result;
 }