获取 User.identity 的名字和姓氏

2024-01-01

我有一个使用 Windows 身份验证设置的 Intranet 应用程序。 我需要在标题中显示用户名和用户的姓名首字母,例如:

欢迎j史密斯JS

到目前为止我做了什么:

<div class="header__profile-name">Welcome <b>@User.Identity.Name.Split('\\')[1]</b></div>
<div class="header__profile-img">@User.Identity.Name.Split('\\')[1].Substring(0, 2)</div>

问题是用户名不是always名字的第一个字母 + 姓氏,有时用户名可以是名字 + 姓氏的第一个字母,例如:

约翰·史密斯 - 用户名can be jsmith但有时也可以 是:johns

在这种情况下,我的代码是错误的,因为它将导致:

jo代替js

如何获取完整的用户名:名字和姓氏User.identity?

然后,我将我的代码基于完整的用户名(名字和姓氏),以便设置首字母缩写,而不是基于并不总是一致的用户名。


在 ApplicationUser 类中,您会注意到一条注释(如果您使用标准 MVC5 模板):“在此处添加自定义用户声明”。

鉴于此,添加 FullName 的样子如下:

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }

    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);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FullName", this.FullName));
        return userIdentity;
    }
}

使用此功能,当有人登录时,FullName 声明将被放入 cookie 中。您可以创建一个助手来访问它,如下所示:

public static string GetFullName(this System.Security.Principal.IPrincipal usr)
{
    var fullNameClaim = ((ClaimsIdentity)usr.Identity).FindFirst("FullName");
    if (fullNameClaim != null)
        return fullNameClaim.Value;

    return "";
}

Update

Or您可以在创建用户时将其添加到用户的声明中,然后从 User.Identity 中将其作为声明检索:

await userManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));

检索它:

((ClaimsIdentity)User.Identity).FindFirst("FullName")

或者您可以直接获取用户并从 user.FullName 访问它:

var user = await userManager.FindById(User.Identity.GetUserId())
return user.FullName

Update

for intranet你可以这样做:

using (var context = new PrincipalContext(ContextType.Domain))
{
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
    var firstName = principal.GivenName;
    var lastName = principal.Surname;
}

您需要添加对System.DirectoryServices.AccountManagement集会。

您可以像这样添加 Razor 助手:

@helper AccountName()
    {
        using (var context = new PrincipalContext(ContextType.Domain))
    {
        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
        @principal.GivenName @principal.Surname
    }
}

如果您打算从视图而不是控制器执行此操作,则还需要向 web.config 添加程序集引用:

<add assembly="System.DirectoryServices.AccountManagement" />

添加到下面configuration/system.web/assemblies.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取 User.identity 的名字和姓氏 的相关文章

随机推荐

  • Visual Studio“转到定义”菜单选项行为 - 为什么 C# 和 VB 项目之间不一致

    当使用 C 项目在 VS2005 中进行开发时 我右键单击框架方法 属性 类型并选择 转到定义 默认情况下 我们会得到一个新的锁定选项卡 其中包含从框架生成的代码 并适当标记为 来自元数据 然而 当我们在 VB NET 项目中执行相同操作时
  • 在正则表达式中匹配两个单词及其之间的一些字符

    我想在没有时对字符串进行匹配abc后面跟着一些字符 可能没有 并以 com 我尝试了以下方法 abc com or abc com or
  • 如何在 useminPrepare 中为 html 文件中的每个块定义单独的流程?

    我们在 index html 中定义了 2 个块 一个用于第三方库 另一个用于我们的应用程序文件 由于第 3 方库已经缩小 我们只想将它们连接起来 而不是丑化 我怎样才能做到这一点useminPrepare
  • 计算 ggplot2 stat_binhex 中 bin 的百分比

    我正在生成不同组的数据点的 binhex 图 每个组可能有不同的总点数 因此我希望它不是每个 bin 值的绝对点数 而是该组内总点数的百分比 这是我目前正在尝试的 d lt data frame grp c rep a 10000 rep
  • 全局安装 npm

    是否可以全局安装 npm 这是一个好主意吗 我使用 npm install 命令安装了 npm 并能够运行 npm start 然后 将我的项目发布到 github 后 我想确保如果有人克隆它 它也能运行 所以我将它克隆到我机器上的另一个目
  • .Net KeyEventArgs 返回与输入

    在 c net 应用程序中使用以下代码 字符串键 e KeyCode ToString 在 net 1 1中键 输入 在 net 3 5中键 返回 我的问题是它们为什么不同 The Keys枚举具有相同的值Enter and Return
  • Rails 每当 gem:每月 20 日

    我在互联网上搜索了这一点 并且文档并没有真正具体讨论每月的工作 所以我希望这里有人能告诉我如何做到这一点 我已经安装了whenever gem 我需要知道的是正确的语法 every month on gt 20th at gt 02 00
  • 在文件中写入大量数据的最快方法

    我正在尝试创建随机实数 整数 字母数字 字母字符串 然后写入文件直到文件大小达到10MB 代码如下 import string import random import time import sys class Generator def
  • 何时使用 Xcode 分布式构建功能

    我在一个小型 iPhone 开发团队工作 在我们的办公室里 我们在任何时候至少有 4 个 XCode 副本在网络上运行 考虑让每个人都运行它 我们使用标准 WIFI 交换机联网 因此网络速度和延迟不如有线网络 只是想知道 使用分布式构建是否
  • STL容器如何折叠?

    我需要 Haskell 的类似物foldl功能可折叠任何 STL 容器 预期签名如下 template Iterator FoldingFunction Result Result foldl Iterator begin Iterator
  • Angular/SignalR 错误:无法完成与服务器的协商

    对我的服务器使用 SignalR 对我的客户端使用 Angular 当我运行客户端时 我收到以下错误 zone js 2969 OPTIONS https localhost 27967 chat negotiate 0 Utils js
  • 如何捕获每个 PID 的网络数据包?

    有人知道一种简单的方法来要求Linux 显示来自 来自google chrome的每个互联网数据包 或 显示来自 来自PID 10275的telnet进程的每个互联网数据包 吗 telnet 示例不太有用 因为我只能使用wireshark
  • React-native ios Podfile 问题与“use_native_modules!”

    在我的反应本机项目中 电子邮件受保护 cdn cgi l email protection 在我运行的 ios 目录中pod install并得到这个错误 Invalid Podfile file no implicit conversio
  • 允许使用应用程序内主页按钮导航至主页吗?

    我想知道应用程序内主页按钮的实现 该按钮可以将您从任何页面返回到主页 据我记得 WP7 开发指南不允许这样做 但我找不到任何相关的书面信息 有谁知道这写在哪里吗 通常不鼓励使用主页按钮 msdn源 http msdn microsoft c
  • C 中的 Malloc 与结构

    我有一个结构 struct numbers struct char numbers array 1000 struct numbers struct numbers some size 创建struct后 有一个整数作为输入 scanf d
  • 无法在构造函数中分配 this [重复]

    这个问题在这里已经有答案了 我正在尝试合并来自的道具values into this 以下会引发错误 我怎样才能做到这一点 this this values 你可以延长this with 对象 分配 https developer mozi
  • 如何处理自定义 PyYAML 构造函数中的递归?

    PyYAML 可以处理常规 python 对象中的循环图 例如 片段 1 class Node pass a Node b Node a child b b child a We now have the cycle a gt b gt a
  • 使用 Python,如何获取 Google protobuf 消息的二进制序列化?

    我在中看到函数 SerializeAsStringprotobuf Python 文档 http code google com apis protocolbuffers docs reference python google proto
  • selenium webdriver 支持 IE10 Metro?

    selenium webdriver 是否支持 IE10 Metro 默认情况下 测试在桌面模式下运行 有什么办法可以在 Metro 模式下测试吗 不 在撰写本文时 还不支持使用 Metro 界面进行自动化 对不起 然而 Selenium
  • 获取 User.identity 的名字和姓氏

    我有一个使用 Windows 身份验证设置的 Intranet 应用程序 我需要在标题中显示用户名和用户的姓名首字母 例如 欢迎j史密斯JS 到目前为止我做了什么 div class header profile name Welcome