开发者

ASP.NET / VB.NET Check If a (different) User IsInRole

开发者 https://www.devze.com 2023-03-18 22:59 出处:网络
I have an ASP.NET application on our company\'s intranet. And a funky security requirement. I need to check to see if a given username is in a certain role. I cannot use

I have an ASP.NET application on our company's intranet. And a funky security requirement.

I need to check to see if a given username is in a certain role. I cannot use

Page.User.IsInRole("MyDomain\MyGroup")

because

Page.User.Identity.Name
开发者_如何学Go

Returns an empty string. Because of some lovely specifications for this program, I have to keep anonymous access enabled in IIS. Seems to rule out any page.user.identity stuff.

So I did find a way to (at least) get the current user (from System.Environment.UserName), but I need to bounce it against the domain group to see if they're in it. Or, better yet, get a list of users within a given domain so I can check myself. Something like...

Dim UserName as String

UserName = System.Environment.UserName

If User(UserName).IsInRole("MyDomain\MyGroup") Then
    MyFunction = "Success"
End If

-OR -

Dim GroupUsers as String()

GroupUsers = GetDomainUserNames("MyDomain\MyGroup")

Anybody have any ideas?


You can call IsUserInRole from the Roles static class. Here is a sample and some reference materials.

Roles.IsUserInRole(username, rolename);

link: http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.isuserinrole.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消