.NET Core RC2中关于登录的声明

3

我正在将.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();等方法得到结果。


希望这个问题能够得到回答,因为我自己也想知道。 - conterio
我回到电脑前会提供一个解决方案。@JeremyConterio - Rovdjuret
那将是很棒的。 - conterio
@Rovdjuret 你回到电脑旁了吗? - Michael Wheeler
哈哈,我感到有些尴尬,因为我忘记了这个,但当我读到这个时,你让我大笑了。我会立即提供一个带有代码示例的答案!@MichaelWheeler - Rovdjuret
@Rovdjuret 我只是想确保这种情况不会发生在你身上! - Michael Wheeler
1个回答

4

对让大家等待表示抱歉!

在创建用户时,我解决了这个问题。在我的情况下,当用户被创建时,我创建声明并将其存储为与用户相关的关系。 然后,我通过整个过程保持这些值更新,这意味着每次有人更改这些值时,它们也必须在声明表中更新。

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接