简体   繁体   中英

Login page in asp.net and sql server using c#

I have a login page in my project which is working correctly. My database (sql server) has 4 fields in which the last one is 'permission', which has 2 values: Admin, User.

I dont know how I should configure my code so that after login, if you are admin, transfer you to the admin page, otherwise go to user page. It means if 'permission' field of a user in data base is 'admin', transfer to admin panel or page.

my C# code behind is:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class content_btn1 : System.Web.UI.Page
{
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from users_table where Name =@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txtuser.Text);
        cmd.Parameters.AddWithValue("@password", txtpassword.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();

        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {  
            Response.Redirect("btn2.aspx");
        }
        else
        {
            lbllogindetails.Text = "Invalid User or Password !!";
        }
    }
}

you can write this code inside your if condition

 if (dt.Rows.Count > 0)
        { 
       if(dt.Rows[0]["permission"] == "Admin")
        {
         Response.Redirect("admin.aspx");
        }
        else
        {
         Response.Redirect("normal.aspx");
        }
}
if (dt.Rows.Count > 0)
{  
     if (dt.Rows[0]["permission"] == "Admin")
           Response.Redirect("adminpage.aspx");
     else
        Response.Redirect("userpage.aspx");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM