如何让用户定向到一个页面,将另一个用户定向到另一个页面 [英] How to have a user directed to one page and another user directed to another page

查看:82
本文介绍了如何让用户定向到一个页面,将另一个用户定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我试图找出如何编码一个用户而不是另一个用户。我的意思是我是CEO用户,我有一个IALO用户。当CEO登录时,我应该被引导到欢迎CEO页面。当IALO登录时,应该有一个Welcome IALO页面。我将如何在ASP.net中执行此操作?我有两张桌子。一个表有CEO用户,另一个有IALO用户。以下是我的代码:

Hello. I am trying to find out how to code one user over the other. What I mean is I am CEO user and I have a IALO user. When the CEO logs in, I should be directed to Welcome CEO page. When the IALO logs in, there should be a Welcome IALO page. How would I do this in ASP.net? I have two tables. One table has the CEO users and the other has IALO users. Here is the code I have:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
        con.Open();

        string chkUser = "select count(*) from TableSecurity where EmailAddress = 'TextBox1.Text' and Password = 'TextBox2.Text'";
            SqlCommand chkUsercmd = new SqlCommand(chkUser, con);
            int i = Convert.ToInt16(chkUsercmd.ExecuteScalar().ToString());
        if(i<1)
        {


        string insCmd = "Insert into TableSecurity (EmailAddress, Password, Level) values (@EmailAddress, @Password, @Level)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBox1.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBox2.Text);
        insertUser.Parameters.AddWithValue("@Level", TextBox1.Text);

    }
    else
{
    Session["New"] = TextBox1.Text;
    Response.Redirect("Secure.aspx");
}

        


        con.Close();
        con.Open();
       string cmdStr = "select count(*) from TableCEO where EmailAddress='" + TextBox1.Text + "'";
       cmdStr = "select count(*) from TableIALO where EmailAddress='" + TextBox1.Text + "'";
            SqlCommand Checkuser = new SqlCommand(cmdStr, con);
            int temp = Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
            if (temp == 1)
            {
                string cmdStr2 = "Select Password from TableSecurity where Password='" + TextBox2.Text + "'";
                SqlCommand pass = new SqlCommand(cmdStr2, con);
                string password = pass.ExecuteScalar().ToString();
                con.Close();

                if (password == TextBox2.Text)
                {
                    Session["New"] = TextBox1.Text;
                    Response.Redirect("Secure.aspx");
                }
                else
                {
                    Label1.Visible = true;
                    Label1.Text = "Invalid Password!!!";
                }
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Invalid UserName!!!";

推荐答案

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
string uname = TextBox1.Text;
string pswd = Textbox2.Text;
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname =@uname and pswd =@pswd", conn);
cmd.Parameters.Add(new SqlParameter("@uname",uname));
cmd.Parameters.Add(new SqlParameter("@pswd",pswd));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())/*if user exists*/
{                
   string str = dr.GetString(0).ToString();
   dr.Close();
   /*get role of the username provided*/
   SqlCommand cmd2 = new SqlCommand("select role from users where uname=@str",conn);
   cmd.Parameters.Add(new SqlParameter("@str",str));
   SqlDataReader dr2 = cmd2.ExecuteReader();
   if (dr2.Read())
   {
     string role1= dr2.GetString(0).ToString();
     dr2.Close();
     /*redirect user based on the value of the role*/
     if (role1== "user")
         Response.Redirect("user.aspx");
     if (role1== "CEO")
         Response.Redirect("ceo.aspx");
   }
}
else/*user doesnot exist in your database*/
{
  /*Alert her and redirect her to signup page*/
  Response.Write("<script type='text/javascript'>");
  Response.Write("alert('Username does not exists.');");
  Response.Write("document.location.href='SignUp.aspx';");
  Response.Write("</script>");
}


这篇关于如何让用户定向到一个页面,将另一个用户定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆