Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何显示显示名称而不是用户名c#_C# - Fatal编程技术网

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

如何显示显示名称而不是用户名c#,c#,C#,我对web开发和.NET也是新手。我有一个用C#编写的ASP.NET网站。如何使用会话显示当前用户的全名而不是用户名?请帮帮我。这是代码 登录页面后面的代码:- System.Security.Principal.WindowsPrincipal username = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal; string userName = u

我对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();
    }

用这个,它就行了。我假设您正在使用windows身份验证。如果它对您有效,请将其标记为答案,以便其他人看到它并从中获得帮助

using System.DirectoryServices.AccountManagement;

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

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

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