Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
- it should be first page of the site and no other page can access without login or at least ask for login.
- suggest with code if you have any samples, I really need it.
- it must work with sql server
Posted
Updated 26-Mar-11 2:36am
v2
Comments
Dalek Dave 26-Mar-11 8:36am    
Edited for Grammar and Syntax.

XML
<authentication mode="Forms">
            <forms defaultUrl="Secured/Default.aspx" loginUrl="Login.aspx">
      </forms>
        </authentication>



XML
<system.web>
            <authorization>
                <deny users="?"/>
            </authorization>
        </system.web>


in the code behind

C#
protected void btn_Submit_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "UserName")
            {
                if (TextBox2.Text == "Password")
                {
                   // Response.Redirect("~/Secured/Secured2.aspx");
                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
                }
                else
                {
                    Label1.Text = "Enter Correct Password";
                }
            }
            else
            {
                Label1.Text = "Enter Correct User Name";
            }
        }


SQL
Use Forms Authentication for that
try this link
Link [^]
 
Share this answer
 
Comments
Dalek Dave 26-Mar-11 8:36am    
Good Answer.
I think you need to read up a bit on authentication and how it works in an IIS environment. What you are asking for should not be handled only in code and SQL Server. You should incorporate built in IIS authentication processes to achieve your goal.
Check out some of these links.
 
Share this answer
 
Comments
Dalek Dave 26-Mar-11 8:37am    
Good Links.
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
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);

        }

This is one of the best option to secure a webform
 
Share this answer
 
v2
Comments
Dalek Dave 26-Mar-11 8:37am    
Good Call.
[no name] 28-Mar-11 1:09am    
Thank you sir.
bhabatosh ojha 7-Jun-13 8:41am    
I have use text boxes and a submit botton and a check box ,how to create a secure login with using data base,all are details below I have provide---

---------------------------------------------------------------------------------
1.<asp:TextBox ID="txt_UserName"

2.<asp:TextBox ID="txt_Passw"

3.<asp:CheckBox ID="Remember_password" and <asp:Label ID="RememberMeLabel" AssociatedControlID="Remember_password"

4.<asp:ImageButton ID="ImageButton1" OnClick="btn_Login_Click"

-------------------------------------------------------------------------------
Note-All values are there in local database ,I want to direct querying values from database table and redirect another welcome page for adminstrator.

But I want to give a high security...so plz guide me

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