Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login form and after successful login i have directed the user to a web form.The authentication is with the active directory.Then i want to get the loged in user name and alway perform a check if the user is login in the directed page before showing up the page.In web config i have:
XML
<authentication mode="Forms">
                <forms loginUrl="Login.aspx" timeout="2880" />
        </authentication>


in the directed page page i have:

C#
string currentUserName = HttpContext.Current.User.Identity.Name;



 protected void Page_Load(object sender, EventArgs e)
 {

    if (HttpContext.Current.User.Identity.IsAuthenticated)
     {



     }

     else
     {
         Response.Redirect("Login.aspx");
         // action for non-authenticated users.

     }


}
the problem is the that the currentuser name is always null and HttpContext.Current.User.Identity.IsAuthenticated is always false.How can i correct this...any better approch

Thanks
Posted

You get username only when user is authenticated:

C#
string userName = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)
 userName = HttpContext.Current.User.Identity.Name;
 
Share this answer
 
Comments
CodeHawkz 5-Sep-12 23:18pm    
He've mentioned that both are always null, isn't it?
Hey there,
have you used added a reference to the membership provider in the web.config? Also, you get this error most of the time if you are using ASP.Net MVC. Let us know if you are using that.

How to use Membership Provider[^] is a great article on how to embed the membership functionality into your website. Furthermore, if you read this article thoroughly, you do not have to check whether the user is authenticated in the code behind. You can do this by adding a web.config for each directory of the website and allow/deny access to users, roles, etc.

Walkthrough: Creating a Web Site with Membership and User Login[^] << For further reading.

Hope this helps, regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900