Click here to Skip to main content
15,886,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to do Role based authentication in login page,without using without using authentication in web.config file.Is that possible with C# codes?

What I have tried:

C#
<pre>protected void btlogin_Click(object sender, EventArgs e)
        {
            cn.Open();
            SqlCommand cmd = new SqlCommand("Select * from Adduser where Username =@username and Password=@password", cn);
            cmd.Parameters.AddWithValue("@username", txtUserName.Text);
            cmd.Parameters.AddWithValue("@password", txtPwd.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {

                Response.Redirect("Default.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");

            }
        }


My aspx

ASP.NET
<pre><form id="form1" runat="server">
        
        <fieldset style="width: 270px; height: 174px; margin-left: 345px">
<legend class="auto-style1">Login</legend> 
<div class='container'>
<asp:Label ID="Name" runat="server" Text="UserName:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtUserName" runat="server" ValidationGroup="lgn" Height="22px"/>
<asp:RequiredFieldValidator ID="RV1" runat="server" ValidationGroup="lgn"
                            ControlToValidate="txtUserName" 
                            ErrorMessage="Please Enter User Name" 
                            SetFocusOnError="True" style="color: #FF0000">*
</asp:RequiredFieldValidator><br />
</div>
 
<div class='container'>
<asp:Label ID="lblPwd" runat="server" Text="Password:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"  ValidationGroup="lgn"
                         CssClass="pwd" Height="22px"/>
<asp:RequiredFieldValidator ID="RV2" runat="server" 
                            ControlToValidate="txtPwd"
     ValidationGroup="lgn" 
                            ErrorMessage="Your Password" 
                            SetFocusOnError="True" style="color: #FF0000">*
</asp:RequiredFieldValidator><br />
</div>
            <tr>
                <td style="width: 360px"></td>

                <td>
                    <asp:Button ID="btlogin" runat="server" Text="Login"  ValidationGroup="lgn" OnClick="btlogin_Click" />
                    <asp:Button ID="btnsp" runat="server" Text="SignUp" OnClick="btnsp_Click" />

                </td>

            </tr>
        
    </form>


HTML
<pre>The below are the ids of the pages::

              
            

                 <a id="menu1" href="Default.aspx">Home</a>
                <a id="menu2" href="Add%20User.aspx">Add User</a>
                <a id="menu3" href="Registrationpage.aspx">Register Employee</a>
                <a id="menu4" href="Contact.aspx">Contact</a>
Posted
Updated 2-Feb-17 23:54pm
v2

1 solution

The user has an option to enter the "Default.aspx" url directly in the browser address bar, by doing so the security fails.
To avoid this situation you will have to store the authenticated key in a session and use it across the page load method of all the pages for validation.

Login page
C#
da.Fill(dt);
             Session["isValidUser"] ="0";
            if (dt.Rows.Count > 0)
            {
                Session["isValidUser"] ="1";
                Response.Redirect("Default.aspx");
            }


Other page:

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["isValidUser"] !="1")
            {
                Response.Redirect("ErrorPage.aspx");
            }
            // your code....
 
Share this answer
 
Comments
Member 12605293 3-Feb-17 5:52am    
Hi Karthik
Thanks for your Quick reply.Whats are the values 0 and 1.Hope those are the ids of the admin and user,and I want the other user to access the Default and Contact.aspx pages,So using (Response.Redirect) will direct one particular pages.I dont want to show other menus except Default and contact pages.Better it should not come inside my header lineup
ZurdoDev 3-Feb-17 10:46am    
Or create a class that inherits from System.Web.UI.Page and put it in there and then have all your webpages inherit from it instead.

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