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);
}
else
{
FormsAuthentication.SetAuthCookie(userName, false, "AdminPart/AdminHome.aspx");
Response.Redirect("HomePage.aspx");
}
}
}
And my web.config file is like this:
<system.web>
<authorization>
<deny users="?" />
</authorization>
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>
</authentication>
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.
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.
User.Identity.IsAuthenticated
- Jeremy Thompson 2012-04-04 00:14