using System; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin.Security; namespace Umbraco.Web.Security.Identity { public static class AuthenticationManagerExtensions { 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 }; } /// /// Extracts login info out of an external identity /// /// /// /// public static async Task GetExternalLoginInfoAsync(this IAuthenticationManager manager, string authenticationType) { if (manager == null) { throw new ArgumentNullException("manager"); } return GetExternalLoginInfo(await manager.AuthenticateAsync(authenticationType)); } } }