简体   繁体   English

mvc3 中的条件(基于授权)标记

[英]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.我正在寻找一种使用授权有条件地显示某些标记的方法,而不仅仅是 User.Identity.IsAuthenticated。

Since I'm using windows authentication with AD, I'd like to be able to do something like an authorizationattribute directly in the markup, like:由于我在 AD 中使用 windows 身份验证,因此我希望能够直接在标记中执行类似授权属性的操作,例如:

@[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?您是否已配置 RoleManager 以支持您的 AD 角色? 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.在 web.config 中,您应该在system.web中有一个roleManager元素,这将允许您配置使用 AD 角色。 Specifically, you will need to enable the manager ( enabled="true" ) and create/specify the provider.具体来说,您需要启用管理器 ( enabled="true" ) 并创建/指定提供程序。

Note that the first approach ( AuthorizeAttribute ) can only be applied to controllers and methods, not within your markup.请注意,第一种方法( AuthorizeAttribute )只能应用于控制器和方法,而不是在您的标记中。 The second approach ( User.IsInRole() ) should work for you, though, once everything is configured correctly.但是,一旦一切都正确配置,第二种方法( User.IsInRole() )应该适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM