为母版页/无 Machine.Config 编写自定义角色提供程序
我想为我的应用程序编写一个自定义角色提供程序,它具有现有的用户和角色表。 我正在寻找相关参考,其中:
- 不需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
权限配置和提供者本身可以在 web.config 中定义。 要将权限应用于不同的子页面,您只需通过 web.config 中的 Location.System.Web.Authorization 节点锁定内容页面(更多信息 此处)。
要创建自定义提供程序,您只需从 (System.Web.Security)RoleProvider 抽象类继承并实现您需要的方法(通常是 IsUserInRole、GetUsersInRole 和 GetRolesForUser,尽管我目前对 Asp. Net 开箱即用的调用来进行基于角色的授权,因此您可能想要全部实现)。 更多信息此处。
完成后,您可以在 web.config 中注册要使用的提供程序:
这会将您的应用程序设置为使用您的角色提供程序,并且几乎不需要任何工作,您就可以启动并运行。 所有标准授权方法仍然有效 (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:
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.
您还可以尝试使用 HttpModule:
- 修改示例应用程序中的代码,以便请求知道要请求哪个页面 - 显然您需要以下 DbTables 结构:
- 这个链接将为您提供一个良好的开始
现在这个粗略的创建表语句将为您提供以下一组:
一些围绕该想法的 DDL SQL:
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 :
Some DDL SQL around the idea:
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 ; )