0

我有一个简单的注册页面,在提交页面内容(用户名和密码)后,我需要为用户分配一个角色。我从 WSAT GUI 创建了 2 个角色(管理员和成员)。

那我怎么给他分配“成员角色”

这是我的注册表..

protected void Button1_Click(object sender, EventArgs e)
    {
        string EPass = Helper.ComputeHash(TextBox2.Text, "SHA512", null);
        String var = System.Configuration.ConfigurationManager.ConnectionStrings["KKSTechConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(var);
        String str = @"insert into Users (Username, Pass, EmpID, Rolename)
                                    values(@Username, @Pass, @EmpID, @Rolename)";
        SqlCommand cmd = new SqlCommand(str, conn);
        cmd.CommandText = str;
        cmd.CommandType = CommandType.Text;

        try
        {
            var userName = TextBox1.Text;
            conn.Open();
            cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Pass", EPass.ToString());
            cmd.Parameters.AddWithValue("@EmpID", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("@Rolename", DropDownList2.SelectedValue); 
            cmd.ExecuteNonQuery();


        }

        finally
        {
            conn.Close();
        }

    }

dbo.Users:用户名(PK)| 通行证 | EmpID (FK) | 角色名

4

1 回答 1

0

As i got you right Roles.AddUserToRoles will help you.

var userName = TextBox1.Text;
Roles.AddUserToRole(userName, "member"); 

To get users in role you can use Roles.GetUsersInRole (Note: it will returns their names only).

PS

It better to use standart membership from the box. My previous answer can help you.

于 2012-08-16T11:20:02.283 回答