.NET Core RC2 中的登录声明

Claims on signin in .NET Core RC2

我正在将 .NET 4.6 版本移植到 .NET Core RC2 并想知道如何在 .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;
}

然后是 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;
    }
}

这给了我使用 User.Identity.ProfileImg(); 等的结果。

抱歉让大家久等了!

我在创建用户时通过执行以下操作解决了这个问题。在我创建用户的情况下,我创建了声明,这些声明存储为与用户的关系。 然后我在整个过程中不断更新这些值,这意味着每次有人更改这些值时,他们也必须在声明中更新 table。

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;
        }
    }

}

然后我可以使用在我看来是这样的例子

@using Microsoft.AspNetCore.Identity
@using Core.Extensions
@{
    ViewData["Title"] = "Overview";
}
<h4 class="mt-0 mb-5">Welcome back @User.Identity.FullName()</h4>