Get info of authenticated user (not computer name)

Go To


I have a login page and it calls following event handler:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    if (Login1.UserName == "abc" && Login1.Password == "1234"){
        if (Request["ReturnUrl"] != null)
            FormsAuthentication.SetAuthCookie(userName, false, "AdminPart/AdminHome.aspx");
            FormsAuthentication.RedirectFromLoginPage(userName, Login1.RememberMeSet);
            FormsAuthentication.SetAuthCookie(userName, false, "AdminPart/AdminHome.aspx");

And my web.config file is like this:

    <deny users="?" />

Now, I'm trying to get authenticated user's user name in my HomePage.aspx.cs, but User.Identity.Name returns the name of my computer. I tried to add following code to config file:

<authentication mode="Forms">
    <forms loginUrl="~/login.aspx" timeout="10" protection="All"></forms>

But a problem occured again with the line <authentication mode="Forms">. After I add this line, in my HomePage, IDE gave me a warning for the first line starting with <% Page ... %> that there would be a runtime error.

I used HttpContext.Current.User.Identity.Name, doesn't work also. I'm just trying to get the name of authenticated user in different parts of project. So I can change the logic if I have to. Any help will be appreciated.

Edit: I'm using Visual Web Devepoler 2008 Express, on Windows 7(64-bit).

EDIT2: Problem resolved somehow. Answer is below. How? Why? Can't it be done programitically? I don't know and I wanna know why.

2012-04-03 23:43
by mtyurt
User.Identity.Name might be empty if Anonymous Authentication is enabled in IIS. Whats the result of this User.Identity.IsAuthenticated - Jeremy Thompson 2012-04-04 00:14
It's true. But something totally awkward occured, I'm updating the post now - mtyurt 2012-04-04 00:37
Since my reputation is low I have 2 hours to answer my question. Till then I edited the question - mtyurt 2012-04-04 05:29
I upvoted you, didn't realise the 2 hour rule. Dont worry tho with the quality of your questions/troubleshooting it wont be till your points go up - Jeremy Thompson 2012-04-04 05:41


Here is the solution I found: first, I removed the authorization tag in web.config file. Then in solution explorer window, I clicked Properties icon and a configuration web page for ASP.NET opened. Then I clicked Security tab->Set up authentication. I chose the option with Internet connection, now I can reach the authenticated user name by Page.User.Identity.Name anywhere.

2012-04-04 07:48
by mtyurt