没有提供商的角色管理?

发布于 2024-11-06 12:59:35 字数 218 浏览 4 评论 0原文

在我的应用程序中,我需要检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写给空气的情书 2024-11-13 12:59:35

如果您想使用 ASP.NET 成员资格/身份验证/授权服务的一部分,您需要实现自定义角色提供程序来执行角色成员资格检查。

首先要做的是创建一个继承自 System.Web.Security.RoleProvider 的类,其中听起来您最初关心实现的方法是:

  • FindUsersInRole
  • GetRolesForUser
  • GetUsersInRole
  • IsUserInRole

因此,您'最终会得到类似于以下内容的内容:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Security;

public class MyCustomRoleProvider : RoleProvider
{
    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
    }

    public override string[] GetRolesForUser(string username)
    {
    }

    public override string[] GetUsersInRole(string roleName)
    {
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        return GetUsersInRole(roleName).Contains(username);
    }
}

注意:Visual Studio 将显示很多方法,例如 GetAllRolesthrows new NotImplementedException(),但我之前已经编写了一个“最低限度”的角色提供程序,并且只需要实现我上面列出的方法。这适用于“只读”角色,但网络应用程序并未更新它们。

然后,您需要将 roleManager 元素添加到 web.configsystem.web 下创建 code> 文件,如下所示:

<roleManager defaultProvider="NameOfYourRoleProvider" enabled="true">
    <providers>
        <clear />
         <add name="NameOfYourRoleProvider" type="Namespace.To.Your.Class.And.Class.Name, Name.Of.Assembly.Containing.Your.RoleProvider" />
    </providers>
</roleManager>

需要记住的一件事是 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:

  • FindUsersInRole
  • GetRolesForUser
  • GetUsersInRole
  • IsUserInRole

So, you'll end up with something similar to:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Security;

public class MyCustomRoleProvider : RoleProvider
{
    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
    }

    public override string[] GetRolesForUser(string username)
    {
    }

    public override string[] GetUsersInRole(string roleName)
    {
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        return GetUsersInRole(roleName).Contains(username);
    }
}

Note: Visual Studio will show a lot of methods, such as GetAllRoles with a throws 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 your web.config file under system.web as follows:

<roleManager defaultProvider="NameOfYourRoleProvider" enabled="true">
    <providers>
        <clear />
         <add name="NameOfYourRoleProvider" type="Namespace.To.Your.Class.And.Class.Name, Name.Of.Assembly.Containing.Your.RoleProvider" />
    </providers>
</roleManager>

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 via HttpContext.Current.Session (and checking that HttpContext.Current isn't null prior to using it), which will require a using System.Web; in the code for your provider.

神经大条 2024-11-13 12:59:35

您的问题不太清楚,但是如果您想提供自己的逻辑来决定用户属于哪些角色,那么您可以按如下方式执行操作:

使用您自己的自定义逻辑填写下面的两个方法:

public class MyRoleProvider : System.Web.Security.RoleProvider
{
    public override string[] GetRolesForUser(string username)
    {
          // check a database or an xml file etc.
    }

    public override bool IsUserInRole(string username, string roleName)
    {
         // check a database or an xml file etc.
    }    
}

然后,添加以下是您的 web.config:(

<roleManager enabled="true" defaultProvider="MyRoleProvider">
  <providers>
    <clear />
    <add name="MyRoleProvider" type="MyNameSpace.MyRoleProvider, MyProjectOrAssemblyName" />
  </providers>
</roleManager>

根据名称替换值)

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:

public class MyRoleProvider : System.Web.Security.RoleProvider
{
    public override string[] GetRolesForUser(string username)
    {
          // check a database or an xml file etc.
    }

    public override bool IsUserInRole(string username, string roleName)
    {
         // check a database or an xml file etc.
    }    
}

Then, add the following to your web.config:

<roleManager enabled="true" defaultProvider="MyRoleProvider">
  <providers>
    <clear />
    <add name="MyRoleProvider" type="MyNameSpace.MyRoleProvider, MyProjectOrAssemblyName" />
  </providers>
</roleManager>

(replacing values according to their name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文