如何在运行时设置角色提供者?

发布于 2024-08-06 00:22:52 字数 113 浏览 3 评论 0原文

我需要能够在运行时设置 RoleProvider。我什至不知道它来自哪里——我正在使用一些松散耦合的依赖注入——所以我什至无法在 web.config 文件中定义它。

如何在运行时设置角色提供者?

I need to be able to set a RoleProvider at runtime. I don't even know where it is coming from -- I am using some loosely coupled dependency injection -- so I can't even define it in the web.config file.

How do I set the role provider at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

毅然前行 2024-08-13 00:22:52

我将创建一个自定义 RoleProvider。该角色提供程序是唯一需要在 web.config 中注册的提供程序。

在 Initialize 方法中,您可以在代码中加载您想要的任何提供程序,并将其存储为自定义提供程序的私有成员。

从那时起,您的角色提供者就只是您选择的提供者的代理。
示例:

public class CustomRoleProvider : RoleProvider
{
    //The real role provider;
    private RoleProvider _provider;

    public CustomRoleProvider()
    { }

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        base.Initialize(name, config);
        //In here initalise your Role Provider at run time
        //This is just demo code... obviously you would do something a little more
        //intelligent
        SqlRoleProvider provider = new SqlRoleProvider();
        provider.Initialize("sql", config);
        _provider = provider;
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        _provider.AddUsersToRoles(usernames, roleNames);
    }
    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        return _provider.DeleteRole(roleName, thrownOnPopulatedRole);
    }
    //other proxy methods would also be here...
}

您可以看到如何扩展它以支持多个提供程序,并在运行时选择可以使用哪些提供程序。

这能回答你的问题吗?

I would create a custom RoleProvider. This role provider is the only provider that needs to be registered in the web.config.

In the Initialize method you can load whichever provider you wanted to in code and store it as a private member of your custom provider.

From that point on your Role Provider is simply a proxy for your chosen provider.
Example:

public class CustomRoleProvider : RoleProvider
{
    //The real role provider;
    private RoleProvider _provider;

    public CustomRoleProvider()
    { }

    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        base.Initialize(name, config);
        //In here initalise your Role Provider at run time
        //This is just demo code... obviously you would do something a little more
        //intelligent
        SqlRoleProvider provider = new SqlRoleProvider();
        provider.Initialize("sql", config);
        _provider = provider;
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        _provider.AddUsersToRoles(usernames, roleNames);
    }
    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        return _provider.DeleteRole(roleName, thrownOnPopulatedRole);
    }
    //other proxy methods would also be here...
}

You could see how you might expand this to support multiple providers and choose at run time which of your providers you could use.

Does that answer your question?

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