I'm porting a .NET 4.6 version to .NET Core RC2 and wondering how to do following in .NET Core RC2.
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim("FullName", string.Format("{0} {1}", this.Firstname, this.Lastname)));
userIdentity.AddClaim(new Claim("Organization", this.Organization.Name));
userIdentity.AddClaim(new Claim("Role", manager.GetRoles(this.Id).FirstOrDefault()));
userIdentity.AddClaim(new Claim("ProfileImage", this.ProfileImageUrl));
// Add custom user claims here
return userIdentity;
}
and then a extension method for Identity.
public static class IdentityExtensions
{
public static string FullName(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("FullName");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
public static string Organization(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Organization");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
public static string Role(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Role");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
public static string ProfileImage(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("ProfileImage");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
}
Which gives me the result of using User.Identity.ProfileImg();
etc..