我正在将.NET 4.6版本移植到.NET Core RC2,并想知道如何在.NET Core RC2中执行以下操作。
Failure
然后是Identity的扩展方法。
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;
}
这给了我使用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;
}
}
等的结果。
答案 0 :(得分:4)
很抱歉让你们等一下!
我在创建用户时通过执行以下操作解决了这个问题。在我创建用户的情况下,我创建了作为与用户的关系存储的声明。 然后我在整个过程中保持这些值的更新,这意味着每当有人更改值时,它们必须在声明表中更新。
var email = /^[a-z0-9'*+\/=?^_`{|}~-]+(?:\.[a-z0-9'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
if (!s.match(email)) return false;
if (s[0] == '-') return false;
if (s[s.length-1] == '-') return false;
if (s.indexOf('--') != -1) return false;
return true;
最后但并非最不重要的是,我创建了扩展,以便为视图和控制器中当前登录的用户访问这些扩展。
var user1 = new ApplicationUser()
{
Firstname = "MyName",
Lastname = "MyLastname",
UserName = "name@domain.se",
Email = "name@domain.se",
EmailConfirmed = true,
PhoneNumber = "000000000",
OrganizationId = organization.Id,
ProfileImageUrl = "user.jpg"
};
await userManager.CreateAsync(user1, "Qwerty1!");
await userManager.AddToRoleAsync(user1, "SuperAdmin");
var claims1 = new List<Claim> {
new Claim("Email", user1.Email),
new Claim("FullName", string.Format("{0} {1}", user1.Firstname, user1.Lastname)),
new Claim("Organization", organization.Name),
new Claim("Role", "SuperAdmin"),
new Claim("ProfileImage", user1.ProfileImageUrl)
};
await userManager.AddClaimsAsync(user1, claims1);
然后我可以在我的视图中使用这样的例子
using System.Security.Claims;
using System.Security.Principal;
namespace Core.Extensions
{
public static class IdentityExtension
{
public static string FullName(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("FullName");
return (claim != null) ? claim.Value : string.Empty;
}
public static string Organization(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Organization");
return (claim != null) ? claim.Value : string.Empty;
}
public static string Role(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Role");
return (claim != null) ? claim.Value : string.Empty;
}
public static string ProfileImage(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("ProfileImage");
return (claim != null) ? claim.Value : string.Empty;
}
public static string Email(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Email");
return (claim != null) ? claim.Value : string.Empty;
}
}
}