简体   繁体   中英

How to get windows username

We want to host the application in IIS. Our application will be used by the Intranet users.

Our main requiremnet is that the windows prompt should not come for intranet users.So we have not used windows authentication. 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)

First approach - Several codes like - " System.Security.Principal.WindowsIdentity.GetCurrent().Name " - does not give username when hosted in IIS.

Second approach - The below code is giving me username through 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. I am not getting the user name.

With out using windows authentication is there any way to get machine username in IIS?

Please guide.

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

string username = this.User.Identity.Name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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