I wrote a custom authorization method for a simple C#.net (VS 2008 web application project). The method will redirect current user to friendly AccessDenied page when they are not authorized. (Using Windows auth). There are 6 users who will be allowed to view this page. The problem is that 1 of the 6 users, user2 is being redirected to the AccessDenied page every time, when they are in fact authorized. I have ruled out spelling and punctuation errors -that is not the issue.
It’s a simple 2 page app, main Default page and the redirect to AccessDenied page. The app works for this one user if I comment out the AuthenticateCurrentUser() method. It works either way for all the other users. Why would only one user be affected? I had this user log on to another computer and try – same result. I then moved the code with the commented out AuthenticateCurrentUser () method into the folder on server and it worked for them.
Should I be authenticating in another way? I’ve seen some examples but they seem overkill for this simple app. There is a no global.asax file - but maybe I should use one and authenticate there instead?
web.config:
<authentication mode="Windows"/>
<authorization>
<allow users="domain\user1, domain\user2, domain\user3, domain\user4, domain\user5, domain\user6" />
<deny users ="*"/>
</authorization>
code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Code to load GridViews
}
AuthenticateCurrentUser();
}
protected void AuthenticateCurrentUser()
{
System.Security.Principal.WindowsIdentity Ident = System.Security.Principal.WindowsIdentity.GetCurrent();
string strUser = Ident.Name.Substring(4);
if ( !(strUser == "user2" || strUser == "user4" || strUser == "user1" || strUser == "user3" || strUser == "user5" || strUser == "user6") )
{
Response.Redirect("AccessDenied.aspx");
}
else
{
Label1.Text = "Access Granted";
}
}
UPDATE: Based on comments I have replaced these 2 lines of code:
System.Security.Principal.WindowsIdentity Ident =
System.Security.Principal.WindowsIdentity.GetCurrent();
string strUser = Ident.Name.Substring(4);
with this single line of code:
string strUser = Environment.UserName;
I will not get to test it on user2 until Monday 4/27/15. Will report back!
FOLLOW UP 4/27/15: The code change made no difference. user2 still cannot access the application deployed on server. I even went so far as to have user2 log on to my local dev machine (where VS 2008 is installed) and ran the debugger as user2. The application correctly allowed user2 access to home page of web app in this scenario. I also had user2 access (remote) server files from my local and she was not allowed access to home page of app. I then had user2 log on from another machine and she still could not access. The server has the exact same code files as the local machine. And this only happens for this user - other users can access just fine, so I am stumped as to why this would occur for this one user when others have no issue. It should happen for all if this were a server configuration and/or permissions issue, shouldn't it?