private static ExternalLoginInfo GetExternalLoginInfo(AuthenticateResult result)
 {
     if (result == null || result.Identity == null)
     {
         return null;
     }
     var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);
     if (idClaim == null)
     {
         return null;
     }
     // By default we don't allow spaces in user names
     var name = result.Identity.Name;
     if (name != null)
     {
         name = name.Replace(" ", "");
     }
     var email = result.Identity.FindFirstValue(ClaimTypes.Email);
     return new ExternalLoginInfo
     {
         ExternalIdentity = result.Identity,
         Login = new UserLoginInfo(idClaim.Issuer, idClaim.Value),
         DefaultUserName = name,
         Email = email
     };
 }
Exemple #2
0
 public string GetEmail(AuthenticateResult authenticateResult)
 {
     if (authenticateResult != null && authenticateResult.Identity != null)
     {
         IEnumerable<Claim> claims = authenticateResult.Identity.Claims;
         if (claims != null)
         {
             Claim emailClaim = claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Email);
             if (emailClaim != null)
                 return emailClaim.Value;
         }
     }
     return null;
 }
        public void AuthenticateAsync_SetsClaimsPrincipal_WhenOwinAuthenticateReturnsIdentity()
        {
            // Arrange
            string authenticationType = "AuthenticationType";
            IAuthenticationFilter filter = CreateProductUnderTest(authenticationType);
            IIdentity expectedIdentity = CreateDummyIdentity();
            IAuthenticationManager authenticationManager = CreateAuthenticationManager((a) =>
                {
                    AuthenticateResult result;

                    if (a == authenticationType)
                    {
                        result = new AuthenticateResult(expectedIdentity, new AuthenticationProperties(), new AuthenticationDescription());
                    }
                    else
                    {
                        result = null;
                    }

                    return Task.FromResult(result);
                });
            IOwinContext owinContext = CreateOwinContext(authenticationManager);
            HttpAuthenticationContext context;

            using (HttpRequestMessage request = CreateRequest(owinContext))
            {
                context = CreateAuthenticationContext(request);

                // Act
                filter.AuthenticateAsync(context, CancellationToken.None).Wait();
            }

            // Assert
            Assert.Null(context.ErrorResult);
            IPrincipal principal = context.Principal;
            Assert.IsType<ClaimsPrincipal>(principal);
            ClaimsPrincipal claimsPrincipal = (ClaimsPrincipal)principal;
            IIdentity identity = claimsPrincipal.Identity;
            Assert.Same(expectedIdentity, identity);
        }
        private static ExternalLogin GetExternalLogin(AuthenticateResult result)
        {
            ExternalLogin externalLogin = null;

            Claim firstClaim = result?.Identity?.FindFirst(IdentityClaimsNameidentifier);
            if (firstClaim != null)
            {
                string identityName = result.Identity.Name;
                identityName = identityName?.Replace(" ", string.Empty);

                string emailAddress = result.Identity.FindFirst(IdentityClaimsEmailAddressIdentifier)?.Value;
                externalLogin = new ExternalLogin
                {
                    ExternalIdentity = result.Identity,
                    LinkedLogin = new UserLinkedLogin(firstClaim.Issuer, firstClaim.Value),
                    DefaultUserName = identityName,
                    Email = emailAddress
                };
            }

            return externalLogin;
        }
        private async Task<ExternalLoginConfirmationViewModel> GetExternalLoginModel(AuthenticateResult result, ExternalLoginInfo loginInfo)
        {
            string name = null;
            string email = null;
            if (loginInfo.Login.LoginProvider == "Facebook")
            {
                name = GetClaimValue(result.Identity, "urn:facebook:name");
            }
            else if (loginInfo.Login.LoginProvider == "Google")
            {
                name = GetClaimValue(result.Identity, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name");
                email = GetClaimValue(result.Identity, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress");
            }
            else if (loginInfo.Login.LoginProvider == "Twitter")
            {
                var userInfo = await GetUserDataFromTwitter(loginInfo.DefaultUserName);
                if (userInfo != null)
                {
                    name = userInfo.FullName;
                }
            }

            var userName = loginInfo.DefaultUserName + loginInfo.Login.LoginProvider;
            userName += name ?? "";
            Regex rgx = new Regex("[^a-zA-Z0-9]");
            userName = rgx.Replace(userName, "");

            return new ExternalLoginConfirmationViewModel() { UserName = userName, Name = name, Email = email };
        }
 private static void Describe(IOwinResponse res, AuthenticateResult result)
 {
     res.StatusCode = 200;
     res.ContentType = "text/xml";
     var xml = new XElement("xml");
     if (result != null && result.Identity != null)
     {
         xml.Add(result.Identity.Claims.Select(claim => new XElement("claim", new XAttribute("type", claim.Type), new XAttribute("value", claim.Value))));
     }
     if (result != null && result.Properties != null)
     {
         xml.Add(result.Properties.Dictionary.Select(extra => new XElement("extra", new XAttribute("type", extra.Key), new XAttribute("value", extra.Value))));
     }
     using (var memory = new MemoryStream())
     {
         using (var writer = new XmlTextWriter(memory, Encoding.UTF8))
         {
             xml.WriteTo(writer);
         }
         res.Body.Write(memory.ToArray(), 0, memory.ToArray().Length);
     }
 }
Exemple #7
0
 public async Task LoginAsync(ExternalLoginInfo externalLoginInfo, AuthenticateResult authenticateResult)
 {
     User user = await _userManager.FindAsync(externalLoginInfo.Login);
     await _authorisationService.SetAuthCookie(user, false);
     await _authorisationService.UpdateClaimsAsync(user, authenticateResult.Identity.Claims);
 }