获取用户名和姓氏 - c#

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

欢迎jSmith 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>

问题在于,用户名并不总是姓和名的首字母,有时用户名可以是ex的名字+姓的首字母:

John Smith-用户名可以是jsmith,但有时也可以
是:约翰斯

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

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

更新资料

或者您可以在创建用户时将其添加到用户的声明中,然后从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

更新资料

对于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下添加。

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

调试捆绑和版本化的javascript文件 - javascript

我已经使用Bundleconfig.cs将我的JavaScript文件打包如下:bundles.Add(new ScriptBundle("~/bundles/resultscripts").Include( "~/Scripts/spectrum.js", "~/Scripts/notify.js"…

在Asp.Net MVC Razor中将HTML视图作为电子邮件附件发送 - javascript

我目前在Razor ASP.Net MVC工作。在这里,我有一个HTML view,名为“客户付款”,它是根据某些计算在RAZOR中生成的,并显示在HTML dialog中。我想将此Html View作为电子邮件附件发送。但是在这里,我感到困惑,我不得不将这个HTML View转移到.PDF or .jpg中的某些jQuery中,然后将其发送到Control…