如何显示显示名称而不是用户名c#

时间:2016-07-21 07:31:06

标签: c#

我也是Web开发和.NET的新手。我有一个使用C#编写的ASP.NET网站。如何使用会话显示当前用户的全名而不是用户名?请帮我。这是代码。

登录页面后面的代码: -

    System.Security.Principal.WindowsPrincipal username = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
    string userName = username.Identity.Name;

    if (username.Identity.IsAuthenticated)
    {
        Session["logged"] = userName;
        agentname.Text = Session["logged"].ToString();
    }
    else
    {
        agentname.Text = "You are not loggd in please logine first ";
    }
    if (Session["logged"] == null)
    {
        agentname.Text = "You are not loggd in please login first ";
    }
    else
    {
        agentname.Text = Session["logged"].ToString();
    }

2 个答案:

答案 0 :(得分:1)

使用它,它有效。我假设您正在使用Windows身份验证。如果它适合你,请将其标记为答案,以便其他人可以看到它并从中获得帮助。

using System.DirectoryServices.AccountManagement;

   string name = UserPrincipal.Current.GivenName + " " + UserPrincipal.Current.Surname;

答案 1 :(得分:1)

您必须安装Windows.Security.Principals NuGet,然后将其写入您的代码中:

私有字符串SessionUsernameText = System.Security.Principal.WindowsIdentity.GetCurrent()。Name

相关问题