0

I have simple registration page, wherein after submitting the contents (Username and Password) of the page, I need to assign the User with a role. I have created 2 roles (Admin and member) from WSAT GUI..

So how'd I assign him with the "member role"

This is my registration form..

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: Username(PK) | Pass | EmpID (FK) | Rolename

Girish
  • 35
  • 1
  • 11

1 Answers1

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.

Community
  • 1
  • 1
user854301
  • 5,383
  • 3
  • 28
  • 37
  • @user854301.. Where do I add this code? In the Submitbtn_Click? Should I create a method for this? – Girish Aug 16 '12 at 11:21
  • Provide code examples that you use plz. And yes, you can add this row to Submitbtn_Click. – user854301 Aug 16 '12 at 11:22
  • Its simple.. I haven't used any of the Authentication methods. As I mentioned, I have used WSAT to create roles and Users. Now I have my own registration form.. Please check my updated post.. – Girish Aug 16 '12 at 11:25
  • @user854301.. Thanks.. How'd I find out the users that I have assigned roles to?? Where do I check? – Girish Aug 16 '12 at 11:33
  • @user854301.. I mean I logged in with the UserName and Password and the form is not letting me in. It says wrong user name and password.. – Girish Aug 16 '12 at 11:39
  • You going in wrong direction, you should use standart `Membership` mechanism that will simplify your authentication/authorization proccess. Now you manage accounts in one place and system know nothing about it, so it works so. I can suggest you to update `CurrentPrincipal` to fix your current issue. This answer can help you http://stackoverflow.com/questions/11658714/authentication-from-session-into-members-folder/11782999#11782999 – user854301 Aug 16 '12 at 11:45
  • I want to use my own registration page.. Reason being, I want the Employee to be brought down in the DropDown list, assign him with Uname and Password and then assign him Role.. This is what I want.. – Girish Aug 16 '12 at 12:00
  • All this logic already written `Membership.CreateUser(String, String)`, `Membership.GetAllUsers()` etc. All you need to create tables in a DB and everything will work. Also you can create your own registration form there is no need to use standart forms or controls. – user854301 Aug 16 '12 at 12:06
  • @User854301.. I have used my own registration form.. All I need to do is Signin him/her in.. But in my DB the Uname and Password exists for emp and as you said I have assigned him Role as well.. I check out in GUI in WSAT, the users are not created :(.. But how'd I Log him in? Very much confused.. – Girish Aug 16 '12 at 12:09
  • GUI in WSAT works with standart `Membership` and `Roles` providers. You will not see any users from you own DB structure there. If you want to see users you should implement your own `MembershipProvider` based on `SqlMemershipProvider`. – user854301 Aug 16 '12 at 12:16
  • So what do you suggest me to do ? I just want simple page to add users to roles.. Just like the one I posted in this question.. – Girish Aug 16 '12 at 12:18
  • Can you create users from GUI in WSAT manually? If yes use std method to create `Membership.CreateUser(String, String)` and to retrive `Membership.GetAllUsers()` – user854301 Aug 16 '12 at 12:31