简体   繁体   English

c#基于角色的登录

[英]c# role based login

I am working on a project that i want to use role based login.我正在处理一个我想使用基于角色的登录的项目。 Ite a really simple project and not for external use at all.这是一个非常简单的项目,根本不供外部使用。 Its just a proof of concept.它只是一个概念证明。 What i had thought about was using a page redirect to an admin page in the page load event.我想到的是在页面加载事件中使用页面重定向到管理页面。 I know this is kinda not the most efficient coding.我知道这不是最有效的编码。 This redirect would be based upon critera in the DB.此重定向将基于数据库中的标准。 I have set a field to "Admin" and if this condition is met then it would redirect to an admin page.我已将一个字段设置为“管理员”,如果满足此条件,它将重定向到管理页面。 So i tried the code below and it doenst work correctly.所以我尝试了下面的代码,它确实可以正常工作。 Any help/input would be appreciated.任何帮助/输入将不胜感激。

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");
    }
}

Try This:尝试这个:

int RowsCount = Convert.ToInt32(com.ExecuteScalar());
if(RowsCount > 0)
      Response.Redirect("Register.aspx");

Suggestion : You need to change your table column name from Admin to UserRole or something which is more readable.建议:您需要将表列名称从Admin更改为UserRole或更具可读性的内容。

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

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