1

I have a login screen that requires a username/password.

I am using ASP.NET's Membership and Role tables that is part of ASP.NET to store info when the user initially registered.

Once the user logs in with the appropriate role, then direct them to another page.

Here is the web.config file for the that other page. Note that it only allows role of SomeRole.

 <configuration> <system.web> <authorization> <allow roles="SomeRole" /> <deny users="*" /> </authorization> </system.web> </configuration> 

In the login screen, I capture the username, password. I am not sure though how to make the following be the user's role.

I have the following code:

 protected void btnLogin_Click(object sender, EventArgs e) { // not sure what code to put here so user logged in that has a Role of SomeRole works with code just below if (User.IsInRole("SomeRole")) 

When I look to see, User's role shows up as blank. My question is, how do I make the current that is logged in have role of SomeRole so that

 <allow roles="SomeRole" /> 

will work. Note that on the login screen, I have a user name and password that belongs to SomeRole but not sure how to make

if (User.IsInRole("SomeRole")) work so that inherit's that user's role. Hope this make sense.

Here is what my web.config looks like:

 <roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="SiRoleProvider" createPersistentCookie="false" cookieProtection="All"> <providers> <clear/> <add connectionStringName="SiiSQL1" applicationName="SiGen" name=" r" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </providers> </roleManager> 

Thanks

7
  • are you using CustomRoleProdiders? which RoleProdider you have in your config file? Commented Apr 21, 2013 at 19:56
  • Hello, updated above with what I have in config file Commented Apr 21, 2013 at 20:00
  • have you assigned/created roles in your system using WAT? or using code? Commented Apr 21, 2013 at 20:03
  • I do that. I use the following code when I register the user: oMU = Membership.CreateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtEmail.Text.Trim()); Membership.UpdateUser(oMU); Roles.AddUserToRole(oMU.UserName, "SomeRole"); Commented Apr 21, 2013 at 20:04
  • I guess what knowing that if the user logs in with a username, password that belongs to Role = "SomeRole", why is if (User.IsInRole("SomeRole")) not taking that role. I believe I need to tell the code somehow Commented Apr 21, 2013 at 20:10

1 Answer 1

2

You need to set authentication cookie for user because you are using custom login control and in order to use Role.IsInRole

Try this in your custom Login button code. SetAuthCookie will actually set logged in user to be accessed by membership provider

protected void Login_Click(object sender, EventArgs e) { if (Membership.ValidateUser(txtUserName.Text, txtPassword.Text)) { FormsAuthentication.SetAuthCookie(txtUserName.Text, true); string url = "~/Member/Default.aspx"; Response.Redirect(url); 
Sign up to request clarification or add additional context in comments.

1 Comment

@NatePet ...that's what I said in this post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.