简体   繁体   English

如何获得Windows用户名

[英]How to get windows username

We want to host the application in IIS. 我们希望将应用程序托管在IIS中。 Our application will be used by the Intranet users. 我们的应用程序将由Intranet用户使用。

Our main requiremnet is that the windows prompt should not come for intranet users.So we have not used windows authentication. 我们的主要要求是内部网用户不应使用Windows提示符,因此我们没有使用Windows身份验证。 We have used forms authentication with active directory authorization. 我们已使用具有活动目录授权的表单身份验证。 But then we are not getting logged in username.(Windows username who has logged in the machine) 但是,然后我们没有登录到用户名。(已登录计算机的Windows用户名)

First approach - Several codes like - " System.Security.Principal.WindowsIdentity.GetCurrent().Name " - does not give username when hosted in IIS. 第一种方法 -诸如“ System.Security.Principal.WindowsIdentity.GetCurrent().Name ”之类的代码-在IIS中托管时不提供用户名。

Second approach - The below code is giving me username through Visual studio 第二种方法-以下代码通过Visual Studio为我提供了用户名

    RegistryKey processor_key = Registry.CurrentUser.OpenSubKey(@"Volatile Environment");
    if (processor_key != null)
    {
        if (processor_key.GetValue("USERNAME") != null)
        {
            Console.WriteLine("Language: {0}", processor_key.GetValue("USERNAME").ToString());
            string username = processor_key.GetValue("USERNAME").ToString();
            txtUsername.Text = username;
        }

    }

But when I am hosting this site in IIS. 但是当我在IIS中托管此站点时。 I am not getting the user name. 我没有得到用户名。

With out using windows authentication is there any way to get machine username in IIS? 在不使用Windows身份验证的情况下,有没有办法在IIS中获取计算机用户名?

Please guide. 请指导。

从MVC控制器(实际上是从System.Web.Mvc.Controller类继承)中,可以访问在基础Controller定义的User对象:

string username = this.User.Identity.Name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM