没有提供商的角色管理?
在我的应用程序中,我需要检查 loggind 用户的角色,以确定用户是否可以看到某些控件,
首先我使用 MS 的登录视图模板,但因为我没有用户数据库,也没有角色数据库 所以我无法添加角色提供者,所以我无法使用角色类来检查用户\角色
就像在我的例子中,我有一个包含用户信息和他所拥有的角色的会话,我需要对这些进行检查角色设置将对用户启用哪些控件,但使用标准方式“使用 .net 内置类或代码”
In my app I need to check the roles for the loggind user to determine if the user could see some controls or not
first I use the loginview template from MS but as I don't have the users db neither the roles db
So I couldn't add a role provider so I couldn't use the Role class to check the users\role
As in my case I have a session with user info and the roles he has,and I need to perform a check on those roles to set which control will be enable to user but with standard way "using .net built in class or code"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用 ASP.NET 成员资格/身份验证/授权服务的一部分,您需要实现自定义角色提供程序来执行角色成员资格检查。
首先要做的是创建一个继承自 System.Web.Security.RoleProvider 的类,其中听起来您最初关心实现的方法是:
因此,您'最终会得到类似于以下内容的内容:
注意:Visual Studio 将显示很多方法,例如
GetAllRoles
和throws new NotImplementedException()
,但我之前已经编写了一个“最低限度”的角色提供程序,并且只需要实现我上面列出的方法。这适用于“只读”角色,但网络应用程序并未更新它们。然后,您需要将
roleManager
元素添加到web.configsystem.web
下创建 code> 文件,如下所示:需要记住的一件事是
RoleProvider
实例是由底层 asp.net 基础结构创建的,因此您需要通过HttpContext.Current.Session
访问会话数据(并在使用之前检查HttpContext.Current
不为 null),这将需要在您的提供商的代码中使用 System.Web;
。If you want to use parts of the asp.net membership/authentication/authorisation services you'll need to implement a custom role provider to perform role membership checking.
The first thing to do is create a class that inherits from
System.Web.Security.RoleProvider
, in which it sounds like the methods you'll initially care about implementing are:So, you'll end up with something similar to:
Note: Visual Studio will show a lot of methods, such as
GetAllRoles
with athrows new NotImplementedException()
, but I've written a "bare minimum" role provider previously and only needed to implement the methods I've listed above. That was for "read only" roles where the web app didn't update them though.You'll then need to add the
roleManager
element to yourweb.config
file undersystem.web
as follows:One thing to bear in mind is that the
RoleProvider
instance is created by the underlying asp.net infrastructure so you'll need to access the session data by going viaHttpContext.Current.Session
(and checking thatHttpContext.Current
isn't null prior to using it), which will require ausing System.Web;
in the code for your provider.您的问题不太清楚,但是如果您想提供自己的逻辑来决定用户属于哪些角色,那么您可以按如下方式执行操作:
使用您自己的自定义逻辑填写下面的两个方法:
然后,添加以下是您的 web.config:(
根据名称替换值)
Your question isn't overly clear, but if you want to provide your own logic for deciding what roles a user belongs to, then you can do that as follows:
Fill in the two methods below using your own custom logic:
Then, add the following to your web.config:
(replacing values according to their name)