为母版页/无 Machine.Config 编写自定义角色提供程序

发布于 2024-07-14 14:41:04 字数 156 浏览 9 评论 0原文

我想为我的应用程序编写一个自定义角色提供程序,它具有现有的用户和角色表。 我正在寻找相关参考,其中:

  • 不需要 machine.config 更改(我在自定义角色体系结构中经常看到这种情况),
  • 支持可应用于需要不同权限的子页面的母版页。

I'd like to write a custom role provider for my application, which has existing users and roles tables. I'm looking for a reference on that, which:

  • doesn't require machine.config changes (I see this a lot in custom role architectures),
  • supports master pages that may be applied to child pages that require different rights.

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

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

发布评论

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

评论(2

凝望流年 2024-07-21 14:41:04

权限配置和提供者本身可以在 web.config 中定义。 要将权限应用于不同的子页面,您只需通过 web.config 中的 Location.System.Web.Authorization 节点锁定内容页面(更多信息 此处)。

要创建自定义提供程序,您只需从 (System.Web.Security)RoleProvider 抽象类继承并实现您需要的方法(通常是 IsUserInRole、GetUsersInRole 和 GetRolesForUser,尽管我目前对 Asp. Net 开箱即用的调用来进行基于角色的授权,因此您可能想要全部实现)。 更多信息此处

完成后,您可以在 web.config 中注册要使用的提供程序:

<configuration>
  <system.web>
    <roleManager enabled="true"
      defaultProvider="YourRoleProviderHere">
      <providers>
        <add name="YourRoleProviderHere"
          type="YourRoleProviderHere, YourRoleProviderAssembly"
          description="Your totally awesome role provider"
        />
      </providers>
    </roleManager>
    ...

这会将您的应用程序设置为使用您的角色提供程序,并且几乎不需要任何工作,您就可以启动并运行。 所有标准授权方法仍然有效 (User.IsInRole),并且您与 Asp.Net 集成。

The configuration of rights and the provider itself can be defined in web.config. To apply the rights to different child pages, you simply lock down the content pages via the Location.System.Web.Authorization node in web.config (more info here).

To create a custom provider, you simply inherit from the (System.Web.Security)RoleProvider abstract class and implement the methods you need (typicaly IsUserInRole, GetUsersInRole, and GetRolesForUser, though my memory's a bit foggy at the moment as to what Asp.Net calls out of the box to do role based authorization, so you you might want to implement them all). More Info here.

Once that's done, you register which provider to use in web.config:

<configuration>
  <system.web>
    <roleManager enabled="true"
      defaultProvider="YourRoleProviderHere">
      <providers>
        <add name="YourRoleProviderHere"
          type="YourRoleProviderHere, YourRoleProviderAssembly"
          description="Your totally awesome role provider"
        />
      </providers>
    </roleManager>
    ...

That will set your app up to use your role provider, and with virtually no work, you're up and running. All the standard authorization methods still work (User.IsInRole) and you're integrated with Asp.Net.

夜无邪 2024-07-21 14:41:04

您还可以尝试使用 HttpModule
- 修改示例应用程序中的代码,以便请求知道要请求哪个页面 - 显然您需要以下 DbTables 结构:
- 这个链接将为您提供一个良好的开始

现在这个粗略的创建表语句将为您提供以下一组:

  • 每个用户将拥有一个或多个 UserRoles
  • 每个页面都可配置为按 UserRole 进行访问

一些围绕该想法的 DDL SQL:

CREATE TABLE [User](
    [UserId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [varchar](100) NOT NULL,
    [SecondName] [varchar](100) NULL,
    [LastName] [varchar](100) NOT NULL,
    [DomainName] [varchar](100) NOT NULL,
    [UserRoleId] [int] NOT NULL,
    [Password] [nvarchar](100) NOT NULL
) ON [PRIMARY]

GO

 CREATE TABLE [dbo].[UserRole](
    [UsersRoleId] [int] IDENTITY(1,1) NOT NULL,
    [RoleId] [int] NOT NULL,
    [UserId] [int] NOT NULL
) ON [PRIMARY]



 CREATE TABLE [ga].[Roles](
    [RoleId] [int] IDENTITY(1,1) NOT NULL,
    [RoleName] [varchar](50) NOT NULL,
    [RoleDefinition] [varchar](1000) NULL
) ON [PRIMARY]

GO

 CREATE TABLE [dbo].[Page](
[PageId] [int] IDENTITY(1,1) NOT NULL,
[PageName] [varchar](200) NOT NULL,
[PageDescription] [varchar](max) NOT NULL,
[PageTitle] [varchar](50) NOT NULL
) ON [PRIMARY]

GO


CREATE TABLE [dbo].[PagePerUserRole](
    [PageForRoleId] [int] IDENTITY(1,1) NOT NULL,
    [UserRoleId] [int] NOT NULL,
    [PageId] [int] NOT NULL
) ON [PRIMARY]

GO

OR CustomBaseClass

基本相同,但会检查用户是否有权访问 asp.net 页面生命周期的某些非常早期的事件 - 例如 OnInit

后者是更非正统的方式 - 但我已经使用复杂的身份验证机制(使用第三软件系统)编写了一个应用程序,并且它似乎在生产中工作了一段时间; )

You could try also to use HttpModule:
- Modify the code in the example app so that the request would know which page is to be requested - obviously you would need the following DbTables structure:
- This link will give you a good start

Now this rough create table statements would give you the following set :

  • each user will have one or more UserRoles
  • each Page will be configurable to be accessed per UserRole

Some DDL SQL around the idea:

CREATE TABLE [User](
    [UserId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [varchar](100) NOT NULL,
    [SecondName] [varchar](100) NULL,
    [LastName] [varchar](100) NOT NULL,
    [DomainName] [varchar](100) NOT NULL,
    [UserRoleId] [int] NOT NULL,
    [Password] [nvarchar](100) NOT NULL
) ON [PRIMARY]

GO

 CREATE TABLE [dbo].[UserRole](
    [UsersRoleId] [int] IDENTITY(1,1) NOT NULL,
    [RoleId] [int] NOT NULL,
    [UserId] [int] NOT NULL
) ON [PRIMARY]



 CREATE TABLE [ga].[Roles](
    [RoleId] [int] IDENTITY(1,1) NOT NULL,
    [RoleName] [varchar](50) NOT NULL,
    [RoleDefinition] [varchar](1000) NULL
) ON [PRIMARY]

GO

 CREATE TABLE [dbo].[Page](
[PageId] [int] IDENTITY(1,1) NOT NULL,
[PageName] [varchar](200) NOT NULL,
[PageDescription] [varchar](max) NOT NULL,
[PageTitle] [varchar](50) NOT NULL
) ON [PRIMARY]

GO


CREATE TABLE [dbo].[PagePerUserRole](
    [PageForRoleId] [int] IDENTITY(1,1) NOT NULL,
    [UserRoleId] [int] NOT NULL,
    [PageId] [int] NOT NULL
) ON [PRIMARY]

GO

OR CustomBaseClass

basically the same but would check whether the use has access on some very early event of the asp.net page life cycle - such as OnInit

The latter is more unorthodox way - yet I have written an app using complicating authentication mechanism ( using 3 -rd software system ) and it seems to work for a while in production ; )

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