我如何设置不同用户的角色 [英] how i set role for diffrent users

查看:64
本文介绍了我如何设置不同用户的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在我的网络项目中

i hv我的数据库中的三种用户1.user,2.super user 3. admin。(我的用户数据库)字段是这样的:ID,Name,User_Name,Password,User_Type)。



现在,当任何用户登录系统时,我该如何检查什么类型的用户他是?? becoz

如果我检查用户类型然后我可以启用管理员用户的编辑/删除选项。用户不能编辑或删除任何东西..



你会帮我提供想法,或者我该怎么办?

解决方案

这里是一个我建立的一个快速应用程序的例子。所以在这里我创建了一个类:



使用System; 
使用System.Collections .Generic;
使用System.Linq;
使用System.Web;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;

///< summary>
///验证用户IsBaseUser
///< / summary>
///
public CL ass IsBaseUser
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings [GridAutoConnectionString]。ToString());

public bool VerifyBaseUser(string uName)
{
using(cn)
{
cn.Open();

使用(SqlCommand cmd = new SqlCommand(SELECT * FROM tblGridAutoUsers WHERE username =''+ uName +''AND rolename =''Base'',cn))
{
SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{
cn.Close();
返回true;
}
else
cn.Close();
返回false;
}
}
}
}





然后我在我的课程中使用了该课程程序:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;


public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}


受保护 void Button1_Click( object sender,EventArgs e)
{
IsBaseUser b = new IsBaseUser();
if (b.VerifyBaseUser(TextBox1.Text)== true
{
Label1.Text = 这是一个基本用户;
}
else
{
Label1.Text = 检查拼写。;
}
}
}





我只是使用了一个按钮,一个文本框和一个标签。这很好用,应该让你朝着正确的方向前进:

 <%@     Page    语言  =  C#    AutoEventWireup   =  true < span class =code-attribute>     CodeFile   =  Default.aspx.cs   继承  =  _默认   %>  

< span class =code-keyword>< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd\">

< html xmlns = http://www.w3.org/1999/xhtm l >
< head runat = server >
< title > < / title >
< / head >
< ; 正文 >
< 表格 id = form1 runat = server >
< div >

< asp:TextBox ID = TextBox1 runat = 服务器 > < / asp:TextBox >
< br / >
< br / > ;
< asp:按钮 ID = Button1 runat = server onclick = Button1_Click 文字 = 按钮 < span class =code-attribute> / >
< br / >
< br / >
< asp:Label ID = Label1 runat = server 文本 = 标签 > < / asp:标签 >

< / div >
< asp:SqlDataSource ID = SqlDataSource1 < span class =code-attribute>
runat = server

ConnectionString = <%


ConnectionStrings:GridAutoConnectionString %>

SelectC ommand = SELECT * FROM [tblGridAutoUsers] > < / asp:SqlDataSource >
< / form >
< / body >
< / html >


Dear All,
In my web project
i hv three type of user in my database 1.user,2.super user 3. admin.(my user database field is like this: ID,Name,User_Name,Password,User_Type).

Now, when any user login in the system then how can i check "what type of user he is?? becoz
if i check the user type then i can enable the EDIT/Delete option for admin user. & the user cant edit or delete any thing..

will u help me to provide idea,,or how can i do this???

解决方案

Here''s an example from a quick app I built. So here you go I created a class:

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

/// <summary>
/// Verify if User IsBaseUser
/// </summary>
/// 
public class IsBaseUser
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["GridAutoConnectionString"].ToString());

    public bool VerifyBaseUser(string uName)
	{
        using (cn)
        {
            cn.Open();
            
            using(SqlCommand cmd = new SqlCommand("SELECT * FROM tblGridAutoUsers WHERE username = ''" + uName + "'' AND rolename = ''Base''", cn))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    cn.Close();
                    return true;
                }
                else
                    cn.Close();
                    return false;
            }
        }
    }
}



Then I used that class in my program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        IsBaseUser b = new IsBaseUser();
        if (b.VerifyBaseUser(TextBox1.Text) == true)
        {
            Label1.Text = "This is a base user";
        }
        else
        {
            Label1.Text = "Check your spelling.";
        }
    }
}



I simply used a button, a textbox, and a label. This works just fine and should get you going in the right direction:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%


ConnectionStrings:GridAutoConnectionString %>" SelectCommand="SELECT * FROM [tblGridAutoUsers]"></asp:SqlDataSource> </form> </body> </html>


这篇关于我如何设置不同用户的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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