Yesterday I was blocked with the problem that how to get the user's alias when he visit my website. Reminded by a friend, he told me that using the IIS Impersonation can implement this function. And then I start to search the reference about the IIS Impersonation, and config my configuraion file, after that I get the user name in the code, but found out that the data was wrong, everytime is return the string 'NT AUTHORITY/IUSR'. The reason is that the user visit the website with Anonymous Authentication. It is apparent that I have configuted the config well, then I try to find out the configuration windows, but fail as the win7 and Server 2008 don't have the same config windows. After a long tangled, I finally get it and implement the feature well. Here I document the config steps down:
Firstly, config your web.config well:
<authentication mode="Windows"/>
<identity impersonate="true" />
Secondly, config the IIS:
For Win7/Server 2008:
1. Click the web site in the IIS
2. Click the Authentication, then set the Anonymous Authentication disabled, ASP.Net Impersonation enabled, the Forms Authentication Disabled, Windows Authentication Enabled.
For Server 2003:
1. Open IIS Manager or add the IIS MMC snap-in to an existing management console.
2. Expand the server that contains the Web site, virtual directory, or file that you want to configure authentication for, config it like above.
FYI, (Reference below is from http://stackoverflow.com/questions/1267071/how-to-get-windows-user-name-when-identity-impersonate-true-in-asp-net)
1) With <authentication mode="Windows"/> in your application and Anonymous access enabled in IIS, you will see the following results:
System.Environment.UserName: Computer Name
Page.User.Identity.Name: Blank
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name
2) With <authentication mode="Windows"/> in your application, and ‘Anonymous access’ disabled and only ‘Integrated Windows Authentication’ in IIS, you will see the following results:
System.Environment.UserName: ASPNET (user account used to run ASP.NET service)
Page.User.Identity.Name: Domain/ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name/ASPNET
3) With <authentication mode="Windows"/> and <identity impersonate ="true"/> in your application, and ‘Anonymous access’ disabled and only ‘Integrated Windows Authentication’ in IIS, you will see the following results:
System.Environment.UserName: Windows Account Name
Page.User.Identity.Name: Domain/ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Domain/ Windows Account Name
Firstly, config your web.config well:
<authentication mode="Windows"/>
<identity impersonate="true" />
Secondly, config the IIS:
For Win7/Server 2008:
1. Click the web site in the IIS
2. Click the Authentication, then set the Anonymous Authentication disabled, ASP.Net Impersonation enabled, the Forms Authentication Disabled, Windows Authentication Enabled.
For Server 2003:
1. Open IIS Manager or add the IIS MMC snap-in to an existing management console.
2. Expand the server that contains the Web site, virtual directory, or file that you want to configure authentication for, config it like above.
FYI, (Reference below is from http://stackoverflow.com/questions/1267071/how-to-get-windows-user-name-when-identity-impersonate-true-in-asp-net)
1) With <authentication mode="Windows"/> in your application and Anonymous access enabled in IIS, you will see the following results:
System.Environment.UserName: Computer Name
Page.User.Identity.Name: Blank
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name
2) With <authentication mode="Windows"/> in your application, and ‘Anonymous access’ disabled and only ‘Integrated Windows Authentication’ in IIS, you will see the following results:
System.Environment.UserName: ASPNET (user account used to run ASP.NET service)
Page.User.Identity.Name: Domain/ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name/ASPNET
3) With <authentication mode="Windows"/> and <identity impersonate ="true"/> in your application, and ‘Anonymous access’ disabled and only ‘Integrated Windows Authentication’ in IIS, you will see the following results:
System.Environment.UserName: Windows Account Name
Page.User.Identity.Name: Domain/ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Domain/ Windows Account Name