Exemplo n.º 1
0
        public void Given_A_Url_And_Parameters_Should_Append_Parameters_To_Url()
        {
            // Arrange
            const string expected = "http://www.dueltank.com/register_confirmation?userId=1231321&token=sdfsf0u8w42029u";

            const string url = "http://www.dueltank.com/register_confirmation?userId=1231321";

            var parameters = new NameValueCollection
            {
                { "token", "sdfsf0u8w42029u" }
            };

            // Act
            var result = UrlHelpers.AppendToReturnUrl(url, parameters);

            // Assert
            result.Should().BeEquivalentTo(expected);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl, string loginUrl, string lockoutUrl,
                                                                string externalLoginUrl, string externalLoginCompleteUrl, string remoteError = null)
        {
            //For "GetExternalLoginInfoAsync" to work on the subsequent request to "ExternalLoginConfirmation", identity cookie must be appended to response.
            //GetExternalLoginInfoAsync is hardcoded to look for the "IdentityConstants.External" cookie,
            var cookieValueFromReq = Request.Cookies[IdentityConstants.ExternalScheme];

            Response.Cookies.Append(IdentityConstants.ExternalScheme, cookieValueFromReq);

            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(Redirect(loginUrl));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var signInResult =
                await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, true, true);

            if (signInResult.Succeeded)
            {
                var email        = info.Principal.FindFirstValue(ClaimTypes.Email);
                var name         = info.Principal.FindFirstValue(ClaimTypes.Name);
                var profileImage = info.Principal.FindFirstValue("profile-image-url");

                if (info.LoginProvider.Equals("Facebook", StringComparison.OrdinalIgnoreCase))
                {
                    var claim = info.Principal.Claims.FirstOrDefault(x =>
                                                                     x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
                    profileImage = "https://graph.facebook.com/" + claim?.Value + "/picture?width=200&height=200";
                }

                var existingUser = await _userManager.FindByEmailAsync(email);

                existingUser.FullName        = name;
                existingUser.ProfileImageUrl = profileImage;

                await _userManager.UpdateAsync(existingUser);

                _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider);

                // Append token to returnUrl
                var externalLoginCompleteUrlParameters = new NameValueCollection
                {
                    { "token", await BuildToken(existingUser) }
                };

                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    externalLoginCompleteUrlParameters["returnUrl"] = returnUrl;
                }

                externalLoginCompleteUrl =
                    UrlHelpers.AppendToReturnUrl(externalLoginCompleteUrl, externalLoginCompleteUrlParameters);

                return(Redirect(externalLoginCompleteUrl));
            }

            // Is the user locked out?
            if (signInResult.IsLockedOut)
            {
                return(Redirect(lockoutUrl));
            }

            var externalLoginUrlParameters = new NameValueCollection
            {
                { "provider", info.LoginProvider }
            };

            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                externalLoginUrlParameters["returnUrl"] = returnUrl;
            }

            externalLoginUrl = UrlHelpers.AppendToReturnUrl(externalLoginUrl, externalLoginUrlParameters);

            return(Redirect(externalLoginUrl));
        }