开发者

Conditional (authorization based) markup in mvc3

I'm looking for a way to conditionally display certain markups using authorization, and not just User.Identity.IsAuthenticated.

Since I'开发者_StackOverflow社区m using windows authentication with AD, I'd like to be able to do something like an authorizationattribute directly in the markup, like:

@[Authorize {Roles = @"domain\Admin"}]
<div> admin markup</div>

I tried using

@if(User.IsInRole(@"domain\Admin"))

but that didn't work either. Can someone give me a pointer to the best way of going about this?


Have you configured the RoleManager to support your AD roles? Note that this is separate from the authentication.

In web.config you should have a roleManager element inside system.web that will allow you to configure for use of AD roles. Specifically, you will need to enable the manager (enabled="true") and create/specify the provider.

Note that the first approach (AuthorizeAttribute) can only be applied to controllers and methods, not within your markup. The second approach (User.IsInRole()) should work for you, though, once everything is configured correctly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜