C#User.IsInRole命名空间 [英] c# User.IsInRole Namespace
问题描述
请注意,我使用C#MVC 3,我想一类,而不是一个控制器中使用。
Note, I am using c# MVC 3, I am trying to use this within a class, NOT a controller.
我有我的计划的顶部以下
I have the following at top of my program
using System.Web.Security;
我试着做以下,但得到的消息:
I tried to do the following but get the message:
名为用户并不在当前上下文中存在。的
下面是我的部分code:
Here is my partial code:
using System.Web.Security;
....
....
if (User.IsInRole("Admin"))
{
}
我想这是必须做一些事情的命名空间,但在看文档,所有我需要的是System.Web.Security。
I am thinking that is has to do something with the namespace but looking at the documentation, all I should need is System.Web.Security.
推荐答案
先尝试:在执行视图中,检查在控制器以下 HttpContext.Current.User.IsInRole (管理)
- 此行来检查你的价值。
Try first : while executing the view, check the following in controller HttpContext.Current.User.IsInRole("Admin")
- this line check your value.
如果您有当前HttpContext加载它应该返回一个布尔
值。
It should return a bool
value if you have current HttpContext loaded.
解决方案2:看一下默认的MVC3项目:
Solution #2: Look at the default mvc3 project:
Context.User.IsInRole("Admin")
而不是 Page.User.IsInRole(管理)
。
除了:您可以检查这个帖子有关如何设置角色的使用 - User.IsInRole( ),而无需使用会员。
In addition: you may check this post about how to set usage of roles - User.IsInRole(" ") without using Membership.
查找以下使用的与 ASP.NET MVC会员
类:
- Membership并授权在ASP.Net MVC 3剃须刀
- SimpleMembership,会员供应商,环球供应商和新的ASP.NET 4.5 Web Forms和ASP.NET MVC 4模板
- Asp.net会员 - 如何确定编程是用户在角色
- ASP.NET MVC成员角色
- Membership and Authorization in ASP.Net MVC 3 Razor
- SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates
- Asp.net membership - how to determine programmatically is user is in role
- ASP.NET MVC Membership Roles
这篇关于C#User.IsInRole命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!