繁体   English   中英

在其他页面中使用其他用户登录?

[英]Login using different user in a different page?

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

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class OnlineAppSyss : System.Web.UI.Page
    {
        private MSConnector connector = new MSConnector();
        //string queries for each DataSet
        string query = null;
        string teacherquery = null;
        string subjectquery = null;
        string schoolfeequery = null;
        string accountdetailsquery = null;
        int rowcounter = 0;
        int teachercounter = 0;

        //DataSet and DataTable initialization
        private DataSet studentData;
        private DataSet subjectData;
        private DataSet schoolfeeData;
        private DataSet teacherData;
        private DataSet accountdetailsData;
        private DataTable subjectTable;
        private DataTable schoolfeeTable;
        private DataTable accountdetailsTable;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (dropdownrole.SelectedItem.Value == "Admin")
            {
                Admin();
            }
            else if (dropdownrole.SelectedItem.Value == "Student")
            {
                Student();
            }
            else if (dropdownrole.SelectedItem.Value == "Teacher")
            {
                Teacher();
            }
        }
        public void Admin()
        {
            //String decleration
            string adminusername = (this.UserName.Value);
            string adminpass = (this.Password.Value);

            try
            {
                if (adminusername == "admin" && adminpass == "cmpe1234")
                {
                    Session["adminlogin"] = adminusername;
                    Response.Redirect("AdministratorPage.aspx");
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }
        }
        public void Student()
        {
            //Connection String
            connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

            //String decleration
            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            //query database from sql server management studio for student
            query = "select studentid,password,firstname,lastname,course,year from student";

            //execute query for student
            studentData = connector.ExecuteQuery(query);


            try
            {
                for (; ; )
                {
                    //string decleration and getting each rows of the Student database
                    string userid = studentData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
                    string password = studentData.Tables[0].Rows[rowcounter]["Password"].ToString();
                    string firstname = studentData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
                    string lastname = studentData.Tables[0].Rows[rowcounter]["LastName"].ToString();
                    string course = studentData.Tables[0].Rows[rowcounter]["Course"].ToString();
                    string year = studentData.Tables[0].Rows[rowcounter]["Year"].ToString();


                    //For Student Condition
                    if (username == userid && pass == password)
                    {
                        //For Student Data Sessions
                        Session["login"] = userid;
                        Session["firstname"] = firstname;
                        Session["lastname"] = lastname;
                        Session["course"] = course;
                        Session["year"] = year;

                        //For Account Details Data
                        accountdetailsquery = "select StudentID,FirstName,MiddleName,LastName,Age,Province,City,Course,Year,College,Department,ContactNumber,Email from student where studentid = " + username + "";

                        //query database from sql server management studio for student as accountDetails Information
                        accountdetailsData = connector.ExecuteQuery(accountdetailsquery);

                        accountdetailsTable = accountdetailsData.Tables[0];
                        Session["AccountDetails"] = accountdetailsTable;

                        //For SchoolFee Data
                        //query database from sql server management studio for schoolfee
                        schoolfeequery = "select DatePaid,AmountPaid,CurrentBalance,TotalBalance,Semester from schoolfee where studentid = " + username + "";

                        //execute query for schoolfee
                        schoolfeeData = connector.ExecuteQuery(schoolfeequery);

                        //get all data rows for SchoolFee and store it into DataTable
                        schoolfeeTable = schoolfeeData.Tables[0];
                        Session["SchoolFee"] = schoolfeeTable;

                        //For Subject Data
                        //query database from sql server management studio for subject
                        subjectquery = "select CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room from subject where studentid = " + username + "";

                        //execute query for subject
                        subjectData = connector.ExecuteQuery(subjectquery);

                        //get all data rows for Subject and store it into DataTable 
                        subjectTable = subjectData.Tables[0];
                        Session["Subjects"] = subjectTable;

                        //Redirect the page to Student Page after the user successfully logs in.
                        Response.Redirect("StudentPage.aspx", true);

                        break;
                    }
                    else
                    {
                        rowcounter++;
                    }
                }

            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
            }

        }
        public void Teacher()
        {
            //Connection String
            connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

            //String decleration
            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            //query database from sql server management studio for student
            teacherquery = "select teacherid,password,firstname,lastname,department,position from teacher";

            //execute query for student
            teacherData = connector.ExecuteQuery(teacherquery);

            try
            {
                for (; ; )
                {
                    //string decleration and getting each rows of the Teacher database
                    string teacheruserid = teacherData.Tables[0].Rows[rowcounter]["TeacherID"].ToString();
                    string teacherpassword = teacherData.Tables[0].Rows[rowcounter]["Password"].ToString();
                    string teacherfirstname = teacherData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
                    string teacherlastname = teacherData.Tables[0].Rows[rowcounter]["LastName"].ToString();
                    string teacherdepartment = teacherData.Tables[0].Rows[rowcounter]["Department"].ToString();
                    string teacherposition = teacherData.Tables[0].Rows[rowcounter]["Position"].ToString();

                    //For Teacher Condition
                    if (username == teacheruserid && pass == teacherpassword)
                    {
                        Session["teacherlogin"] = teacheruserid;
                        Session["teacherfirstname"] = teacherfirstname;
                        Session["teacherlastname"] = teacherlastname;
                        Session["department"] = teacherdepartment;
                        Session["position"] = teacherposition;

                        //Redirect the page to Student Page after the user successfully logs in.
                        Response.Redirect("TeacherPage.aspx", true);
                        break;
                    }
                    else
                    {
                        rowcounter++;
                    }
                }
            }
            catch
            {
                Response.Write("<script language=javascript>alert('Username and password does not match. Try again (teacher)');</script>");
            }
        }
    }
}

我的问题是我无法访问此代码中的教师页面,它将仅访问学生页面。 我应该在系统中使用什么条件来避免冗余?

这是我的aspx下拉列表代码:

<asp:DropDownList runat="server" id="dropdownrole">
    <asp:ListItem Text="Admin">Admin</asp:ListItem>
    <asp:ListItem Text="Student">Student</asp:ListItem>
    <asp:ListItem Text="Teacher">Teacher</asp:ListItem>
</asp:DropDownList>

和登录按钮:

 protected void Button1_Click(object sender, EventArgs e)
        {
            if (dropdownrole.SelectedItem.Value == "Admin")
            {
                Admin();
            }
            else if (dropdownrole.SelectedItem.Value == "Student")
            {
                Student();
            }
            else if (dropdownrole.SelectedItem.Value == "Teacher")
            {
                Teacher();
            }
        }

我希望有一个条件,如果用户名和密码输入检测到2个用户中的任何一个,它将重定向到他们的特定网页。 请帮忙。

您没有在您选择的老师密码teacherquery为此该teacherpassword总是空,之所以条件password == teacherpassword永远是假的。

就像DPac指出的那样,您无需查询所有行就仅需验证用户名和密码,只需仅选择用户名和密码,如果正确,则选择所需的所有行并将其分配给会话,然后重定向。

关于该问题,如果学生的用户名与老师的用户名相同,将会有很多麻烦,要避免该问题(不做很多事情)是在两个名为userRole表中再添加1个int行(例如:1 = admin ,2 =老师,3 =学生等),并将其设置为空。 之后,进入每个表并将该行更新为适当的值(例如,在学生表中: UPDATE student SET userRole = 3 )。 更新所有行之后,请转到“设计”,并将userRole设置为不允许为null现在,用户名+ userRole组合(应该是您的主键)将使您的工作变得更加轻松。

尽管我更喜欢@ronaldinho为两个表提供RoleId列的方法。这是您可以做的而又不会打扰数据库。

只需在登录页面上添加一个包含“教师”和“学生”项目的下拉列表。因此,当有人尝试登录时,他们必须选择自己的角色,然后输入。 这样,您可以将代码定向为

   protected void Button1_Click(object sender, EventArgs e)
    {
        if (dropdownrole.SelectedItem.Text == "Admin")
        {
            Admin();
        }
        else if (dropdownrole.SelectedItem.Text == "Student")
        {
            Student();
        }
        else if (dropdownrole.SelectedItem.Text == "Teacher")
        {
            Teacher();
        }
    }

看看有没有可能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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