Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Membership - Which RoleProvider to use so User.IsInRole() checks ActiveDirectory Groups?

Very simple question actually:

I currently have IIS anonymous access disabled, users are automatically logged on using their Windows login. However calling User.IsInRole("Role name") returns false. I double-checked User.Identity.Name() and the "Role name" and it should return true.

I currently have this in my Web.Config:

UPDATE
I was calling User.IsInRole("Role name") where I should call User.IsInRole("DOMAIN\Role name")

However I still like to know if the <membership> entry is needed at all?

What should I change? (and is the <membership> entry needed at all?)

  <authentication mode="Windows">
      <forms
      name=".ADAuthCookie"
      timeout="10" />
  </authentication>


<membership defaultProvider="ADMembershipProvider">
  <providers>
    <clear/>
      <add
         name="ADMembershipProvider"
         type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="ADConnectionString"
         connectionUsername="XXX\specialAdUser"
         connectionPassword="xx"
         />
  </providers>
</membership>

<roleManager enabled="true" defaultProvider="WindowsProvider">
  <providers>
    <clear />
      <add name="WindowsProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
  </providers>
</roleManager>
like image 526
Ropstah Avatar asked May 14 '09 12:05

Ropstah


People also ask

How to assign roles to users in ASP NET membership provider?

Here we will learn how to assign roles to the users in ASP.NET membership provider. For assigning the roles to the user we need to add a model for member list and roles List. First add a model class in account model.cs class name is “ AssignRolesToUsers ”. [Required (ErrorMessage = " Select proper UserRole Name")]

What happens if isinrole does not find the specified role?

If IsInRole does not find the specified role, it calls the GetRolesForUser method of the default Provider instance to determine whether the user name is associated with a role from the data source for the configured ApplicationName value. Gets a value indicating whether a user is in the specified role.

What is membership provider in ASP NET?

Thank you. The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.

How to check if current user has role name?

using(UsersRoleContext context = new UsersRoleContext ()) var roleName = (from UP in context.UserRoles where UP.RoleId == RoleId select UP.RoleName).SingleOrDefault (); Following code is for checking if current user has role name or not. using(UsersRoleContext context = new UsersRoleContext ())


1 Answers

If you use Windows authentication IsInRole will work with no extra configuration, as long as you remember to prefix the role with the domain, i.e. DOMAIN\groupName.

In addition you can role (pun intended) your own and use Windows auth against, for example, a SQL Role Provider, where you don't want your AD littered with custom roles for your application.

So no, you don't need the provider configuration at all.

like image 130
blowdart Avatar answered Oct 20 '22 10:10

blowdart