English 中文(简体)
How to keep RoleProvider from overriding custom roles?
原标题:

I have an custom role provider that gets the roles a user belongs to from a database. I also have a custom authentication module registered in my web.config s httpModules which sniffs incoming HTTP requests and (if it s an OAuth signed request) sets the HttpContext.Current.User property to impersonate the user, and the IPrincipal that it sets includes all the user s roles, plus an extra one called "delegated".

The trouble is, after I set my custom IPrincipal, apparently ASP.NET still calls my custom role provider, and then resets the IPrincipal with one that has only the standard roles for that user.

If I set <roleManager enabled="false" ...> in my web.config file, the authentication module s assigned roles stick. Obviously though, I want the best of both worlds. How can I use the role provider, but "cancel" the role provider s effect when my authentication module decides to?

最佳回答

It turns out that in the authentication http module s Init method, I can find the RoleManager, and then hook an event that gives me veto power on whether it does its overriding work:

    public void Init(HttpApplication context) {
        var roleManager = (RoleManagerModule)context.Modules["RoleManager"];
        roleManager.GetRoles += this.roleManager_GetRoles;
    }

    private void roleManager_GetRoles(object sender, RoleManagerEventArgs e) {
        if (this.application.User is OAuthPrincipal) {
            e.RolesPopulated = true; // allows roles set in AuthenticationRequest to stick.
        }
    }

    private void context_AuthenticateRequest(object sender, EventArgs e) {
        if (/*oauth request*/) {
            HttpContext.Current.User = CreateOAuthPrincipal();
        }
    }
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签