Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I develop a login and register page in C# and ASP.Net
My website menu is home, login, services. I want when user is not login in and click on
services, the login dialog box appears first
Posted
Updated 25-Mar-11 1:17am
v2
Comments
Johnny J. 25-Mar-11 7:19am    
Pavel has provided a good start for you below. In order for anybody to be able to answer it better for you, they would need to know HOW you have done your login code...

Use Forms Authentication.
Use some thing like this at web.config
XML
<location path="secure">
    <system.web>
      <authorization>
        <deny users="?"/>
        <deny users="jhon"/>
      </authorization>
    </system.web>
  </location>


secure is a folder which contains your secure webforms.
XML
<authentication mode="Forms">
      <forms loginUrl="Default.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH"
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" >
        <credentials passwordFormat="Clear">
          <user name="kim" password="kim@123"/>
          <user name="jhon" password="jhonn"/>
        </credentials>
      </forms>
    </authentication>


Now at server side code

Default.aspx is your login form, Drag Two TextBoxes and a Button
at click event of button write following code. Default2.aspx is destination page. Secure is a folder which can have webforms which you wants to make secure like you are talking about services.


if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text))
        {
            FormsAuthentication.SetAuthCookie(
                 this.TextBox1.Text.Trim(), false);

            FormsAuthenticationTicket ticket1 =
               new FormsAuthenticationTicket(
                    1,                                   // version
                    this.TextBox1.Text.Trim(),   // get username  from the form
                    DateTime.Now,                        // issue time is now
                    DateTime.Now.AddMinutes(10),         // expires in 10 minutes
                    false,      // cookie is not persistent
                    "HR"                              // role assignment is stored
                // in userData
                    );
            HttpCookie cookie1 = new HttpCookie(
              FormsAuthentication.FormsCookieName,
              FormsAuthentication.Encrypt(ticket1));
            Response.Cookies.Add(cookie1);

            // 4. Do the redirect. 
            String returnUrl1;
            // the login is successful
            if (Request.QueryString["ReturnUrl"] == null)
            {
                returnUrl1 = "Default2.aspx";
            }

            //login not unsuccessful 
            else
            {
                returnUrl1 = Request.QueryString["ReturnUrl"];
            }
            Response.Redirect(returnUrl1);

        }
 
Share this answer
 
Comments
Sona s 25-Mar-11 9:06am    
Look nice and very systematic
sawant parag 9-Nov-11 0:30am    
If i 1000 user who had done sucessful authenticated. out of that 500 user should allow to access Default2.aspx and rest 500 user to access the Default.aspx.?
how would i authorize such a large count of user to access the pages?
adding all the user name in the allow user tab is not feasible..
 
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