Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Membership: how to set the user as logged in

I am trying to get the Membership Provider to work.

So far I have:

 <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">  </asp:Login> 

calling :

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) {     if(Membership.ValidateUser(Login1.UserName, Login1.Password))     {         Response.Redirect("/admin/default.aspx");         // Set the user as logged in?     } } 

If I enter the correct login/password, the ValidateUser function returns true. So my question is: how do I set the user as logged in?

I am testing this in my pages doing :

protected void Page_Load(object sender, EventArgs e) {     if ( Membership.GetUser()==null)     {         Response.Redirect("/admin/login.aspx");     }     // else "you are logged in, congratulations"                 } 

I would have used the default functions, but it is just not working and a google search made me think that I will save time by actually recoding all that myself.

Anything will help!

EDIT: Regarding the accepted answer, it is the correct one for "how to set the user as logged in" and works fine. It didn't fixed my specific problem but only a part of it. Thought if you look thought the comments you will find interesting pointers.

EDIT 2 and solution: Ok I finally worked it out thanks to all the comments. Here is what I did, it's simpler than what I expected :

Page that checks login state:

 protected void Page_Load(object sender, EventArgs e)  {      if ( !Request.IsAuthenticated)      {          Response.Redirect("/admin/login.aspx");      }   

Log out:

   protected void LoginStatus1_Logout(object sender, LoginCancelEventArgs e)    {        FormsAuthentication.SignOut();        Response.Redirect("/admin/login.aspx");    } } 

web.config:

<authentication mode="Forms" /> 

login:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) {     if(Membership.ValidateUser(Login1.UserName, Login1.Password))     {         FormsAuthentication.SetAuthCookie(Login1.UserName, true);         Response.Redirect("/admin/default.aspx");      } } 
like image 692
marcgg Avatar asked May 26 '09 19:05

marcgg


People also ask

How does ASP Net membership work?

The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.

What is authentication mode forms?

Forms authentication enables user and password validation for Web applications that do not require Windows authentication. With forms authentication, user information is stored in an external data source, such as a Membership database, or in the configuration file for an application.

What is membership in web config?

The membership element is a sub-element of the system. web section. You can enable ASP.NET Membership for an application by directly editing the Web. config file for that application, or you can use the Web Site Administration Tool, which provides a wizard-based interface.


2 Answers

Put this in Login1_Authenticate before calling Response.Redirect("/admin/default.aspx");

FormsAuthentication.SetAuthCookie("username", true); 
like image 155
Gromer Avatar answered Sep 16 '22 11:09

Gromer


Try moving your code and Gromer's suggestion to the LoggedIn event.

protected void Login1_LoggedIn(object sender, EventArgs e)     {         if(Membership.ValidateUser(Login1.UserName, Login1.Password))         {             FormsAuthentication.SetAuthCookie(Login1.UserName, true);             Response.Redirect("/admin/default.aspx");         }      } 

EDIT: Like Gromer said, only do this if you have to execute some business code after the user is logged in and before s/he is redirected.

EDIT EDIT: Visual Studio describes the Authenticate event as, "called to authenticate the user," which implies that the user is not authenticated before the event is called. Thus, you cannot confirm that the user is logged in because s/he has not been authenticated yet.

like image 31
Matthew Jones Avatar answered Sep 16 '22 11:09

Matthew Jones