1

Just like the title states. Need the windows logon and domain info from within our asp.net page.

I tried

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

but it returns the IIS App Pool not the user name

Thanks

HQCasanova
  • 1,158
  • 1
  • 10
  • 15
RobDog888
  • 239
  • 1
  • 3
  • 14
  • 1
    HTH http://stackoverflow.com/questions/1267071/how-to-get-windows-user-name-when-identity-impersonate-true-in-asp-net – Joe Nov 13 '13 at 20:57
  • Perhaps this? http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx – CDspace Nov 13 '13 at 20:58
  • Where do I set "" in my web.comfig? Also, I did a test and the IsAuthenticated is False. – RobDog888 Nov 13 '13 at 21:08

2 Answers2

4

Try HttpContext.User, accessible simply as User from the code behind. It returns both the domain and username, but should be easy enough to trim for your needs. It's worked for me in the past. You can also use this to manage roles in your application, if you need to.

EDIT
Below are the relevant portions of my web.config. I also used aspnet_regsql.exe to setup the tables needed for the role manager in my database. I could then use User.Identity.Name and User.Identity.IsInRole

<connectionStrings>
    <clear/>
    <add name="SqlRoleManagerConnection"
         connectionString="myConnectionString">
    </add>
  </connectionStrings>

  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?"/>
    </authorization>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <clear/>
        <add name="SqlRoleManager"
             type="System.Web.Security.SqlRoleProvider"
             connectionStringName="SqlRoleManagerConnection"
             applicationName="myAppName" />
      </providers>
    </roleManager>
  </system.web>
CDspace
  • 2,639
  • 18
  • 30
  • 36
  • In order for this to work, you need to either set up the app in IIS or in web.config to not allow anonymous users, or you're going to get a blank. – Tim Nov 13 '13 at 21:25
  • @Tim See edit, added relevant portions of web.config – CDspace Nov 13 '13 at 21:41
0

This is my code in VB.net

var strUser = System.Web.HttpContext.Current.User.Identity.Name

so C# must be in the lines of: (not tested) string strUser = System.Web.HttpContext.Current.User.Identity.Name;

In the Web.Config file

<configuration>
    <system.web>
        <authentication mode="Windows"/>
        <identity impersonate="true" />
    </system.web>
</configuration>
CheGueVerra
  • 7,849
  • 4
  • 37
  • 49