我正在开发一个项目,我想使用基于角色的登录。 我做了一个非常简单的项目而不是外部使用。 它只是一个概念证明。 我想到的是在页面加载事件中使用页面重定向到管理页面。 我知道这不是最有效的编码。 此重定向将基于DB中的critera。 我已将字段设置为“Admin”,如果满足此条件,则会重定向到管理页面。 所以我尝试了下面的代码,它确实正常工作。 任何帮助/意见将不胜感激。
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 System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] != null)
{
lblWelcom.Text += Session["New"].ToString();
if (Session["New"] != null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ShowUsersConnectionString"].ConnectionString);
conn.Open();
string checkAdmin = "select count(*) from Staff where Admin='" + "Admin" + "'";
SqlCommand com = new SqlCommand(checkAdmin, conn);
Response.Redirect("Register.aspx"); //this is the admin page
conn.Close();
}
else
{
Response.Redirect("profile.aspx"); //this is the non admin page
}
}
else
Response.Redirect("Login.aspx");
}
protected void btnLogout_Click(object sender, EventArgs e)
{
Session["New"] = null;
Response.Redirect("Login.aspx");
}
}
答案 0 :(得分:0)
试试这个:
int RowsCount = Convert.ToInt32(com.ExecuteScalar());
if(RowsCount > 0)
Response.Redirect("Register.aspx");
建议:您需要将表格列名从Admin
更改为UserRole
或更具可读性的内容。