.NET Core RC2 中的登录声明

2024-02-07

我正在将 .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(); etc..


抱歉让大家久等了!

我通过在创建用户时执行以下操作解决了这个问题。就我而言,当创建用户时,我创建了声明,这些声明存储为与用户的关系。 然后,我在整个过程中保持这些值的更新,这意味着每次有人更改这些值时,它们也必须在声明表中更新。

var user1 = new ApplicationUser()
{
    Firstname = "MyName",
    Lastname = "MyLastname",
    UserName = "[email protected] /cdn-cgi/l/email-protection",
    Email = "[email protected] /cdn-cgi/l/email-protection",
    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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.NET Core RC2 中的登录声明 的相关文章

随机推荐

  • 从 ImageField Django 2.0 获取 EXIF 数据

    我正在执行提取通过 DJANGO 2 1 2 上传的照片的 exif 数据的任务 这是我的 model py 更新型号 class UploadedImage models Model image models ImageField Upl
  • Shell GNU-Screen -X 的问题

    OPTIONS java Xms1024M Xmx1024M jar craftbukkit jar PROCESS server01 screen dmS PROCESS OPTIONS nogui Starting the applic
  • 高效替换 text2vec 中的单词

    我有一个很大的文本正文 我想有效地用它们各自的同义词替换单词 例如 用同义词 汽车 替换所有出现的 汽车 但我很难找到一种合适的 有效的方法 来做到这一点 为了后面的分析 我使用text2vec库 并且也想使用该库来完成此任务 避免tm以减
  • 如何创建一个 ImageView 填充父级高度并显示尽可能大的 Image?

    我有一个按以下方式定义的 ImageView
  • 从 C# 运行宏 [重复]

    这个问题在这里已经有答案了 我知道你可以使用Microsoft Office Interop Excel 在 C 程序中使用 VBA 命令 我有接近 10 000 行代码的 VBA 将其转换为 C 兼容命令是不现实的 它创建一个工作簿并执行
  • 有没有更好的方法来控制子进程的 PYTHONPATH?

    我有一组必须动态修改 os sys path 的脚本 然后脚本启动一个子进程 理想情况下 子进程应与调用者具有相同的 os sys path 我想避免将其作为参数传递 因为这需要修改子进程脚本 我有可以运行并满足我所有需求的代码 我想知道是
  • 尝试将所有组件更改为es6

    我正在尝试将所有组件更改为 es6 我做了两个 但不知道第三个该怎么做 你能告诉我如何改变它吗 下面提供我的代码 export default class FirstTimeTab extends React Component getIn
  • Twig 与 Symfony 2 显示 prod 和 dev 之间不同的 json 编码变量

    我们正在构建一个 Symfony 2 应用程序 它将一些数据从控制器发送到视图 控制器 user array configuration gt array levels gt array warning gt 0 05 danger gt
  • 缩放数组(矩阵)

    该程序的目的是创建一个更大的字节数组 将原始数组放大 10 倍 例如 0 0 中的 1 应该是新数组中由 1 组成的 10x10 正方形 我提供了代码和输出 它们在填充较大数组期间似乎可以正常工作 但随后会打印不同的值 我目前正在尝试仅使用
  • browser.sleep 和 browser.pause 不会被执行

    我是量角器和打字稿的新手 现在正在尝试 PoC 框架 但是 我想知道为什么 browser sleep 或 browser pause 在以下场景中不被执行 第一步通过后测试立即退出 Given I access the Catalogue
  • 向当前日期添加一个月

    我必须在今天的日期上添加一个月 并且必须获得 1 个月后的日期 有人可以帮忙吗 Dim newDate as DateTime DateTime Now AddMonths 1
  • 套接字失去连接

    我知道 Twisted 可以很好地做到这一点 但是如果只是简单的套接字呢 你如何判断套接字中的连接是否随机丢失 就像 如果我的互联网在一秒钟内停止并重新连接 我假设你正在谈论 TCP 如果您的互联网连接中断了一秒钟 您可能根本不会丢失 TC
  • 从java中的.p7b文件中提取单个.cer证书

    我是密码学新手 如果您认为这是一个基本问题 请原谅 我有一个 p7b 文件 我需要读取并提取各个公共证书 即 cer 文件并将其存储在密钥存储中 我不必担心密钥存储中的持久性 因为已经有一个服务将 cer 文件作为byte 并保存它 我想知
  • mysql_insert_id();成功插入行后不返回值

    我发誓我已经在这个网站和其他网站上倾注了所有其他类似的问题 但我想我只是错过了一些东西 希望有人能指出我的大脑向我隐藏的一个愚蠢的错误 我的脚本将表单中的值插入到名为 notes 的表中 此时 它通过名为 newRelationship 的
  • 查找一天中花费的时间以及所花的工间休息时间

    我现在的情况是 我需要找出一些内部申请在办公室花费的总时间 我有这样的样本数据 Id EmployeeId ScanDateTime Status 7 87008 2018 08 02 16 03 00 227 1 8 87008 2018
  • 如果我不设置缓存过期会发生什么

    我正在 google pagespeed Insights 上测试我的页面 它返回 在静态资源的 HTTP 标头中设置到期日期或最长期限 指示浏览器从本地磁盘而不是通过网络加载以前下载的资源 我的假设是 如果我不设置过期时间 我的文件将永远
  • 如果所有子域都指向网站根目录,是否需要通配符 SSL 证书

    我对 SSL 证书完全陌生 需要很快购买一个 当子域名发挥作用时 价格似乎会大幅上涨 我的问题是这样的 我已经设置了我的网站 以便用户名 domain com通过 htaccess 重写为域名 com user 用户名 如果我将网站设置为所
  • Java ASM 字节码修改-更改方法体

    我有一个罐子里的类的方法 我想与我自己的主体交换 在这种情况下 我只想让该方法将 GOT IT 打印到控制台并返回 true 我正在使用系统加载器来加载 jar 的类 我使用反射使系统类加载器能够通过字节码加载类 这部分似乎工作正常 我正在
  • 如何将列与 Flexbox 对齐?

    我目前正在学习 Flexbox 布局 但找不到解决我的问题的方法 我尝试使用 justify content 和 flex basis 但它不起作用 有人有解决办法吗 The result I would like section disp
  • .NET Core RC2 中的登录声明

    我正在将 NET 4 6 版本移植到 NET Core RC2 并想知道如何在 NET Core RC2 中执行以下操作 public async Task