Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:46+00:00 2026-05-28T04:38:46+00:00

I’m currently using Unity with Unity.Mvc3 in my MVC3 application which uses Windows Authentication.

  • 0

I’m currently using Unity with Unity.Mvc3 in my MVC3 application which uses Windows Authentication. I’m also reading up on doing dependency injection and am trying to set up an Ambient Context for some authorization checking.

I have an abstract AuthorizeContext class with a static property Current that holds one of three implementations of this class:

  1. AuthorizeRoleContext, used for production, which takes a string and an IPrincipal through constructor injection and is just a layer of indirection to call IPrincipal.IsUserInRole whenever a request or demand for authorization is made. The injected string is used as a domain prefix for the role.

  2. AuthorizeContextAllowAll, used for development and sometimes testing, which always allows all requests and demands for authorization, using a default constructor.

  3. AuthorizeContextAllowNothing, used for testing minimum allowed functionality, which always denies all requests for authorization.

  4. In the future, add one more and check authorization through a database…?

So I can manually wire things up by doing (for instance, in Application_BeginRequest):

AuthorizeContext.Current = new
AuthorizeRoleContext(HttpContext.Current.User);

and then call

AuthorizeContext.Current.Demand(“someRole”);

This works great, but I’m looking for the right way (and place) to wire it up with Unity, while avoiding the Service Locator anti-pattern. So my question is: how do I do this?

Some of the challenges I have with Unity registration so far:

  • I do not have an HttpContext.Current.User at Application_Start, so I cannot inject it in AuthorizeRoleContext at that time
  • I do not know how to assign an implementation to the static AuthorizeContext.Current property

Now maybe I should just do [Authorize(Roles = “Role1”)] everywhere and just let MVC3 do its thing, but:

  • I also use it to build up my menu and perhaps want to use it in other places for authorization checking.
  • I’d like to swap out authorization checking with other implementations (like allow all) so I don’t have to assign Windows Groups to each developer/tester.
  • I seem to have to use a domain prefix for the role with IPrincipal.IsUserInRole or it won’t work across dev/test/production. Ofcourse, production is at the customer environment, using totally different domain names. This is the reason I made the domain name configurable and inject it.
  • I’d like to unit test things.

But perhaps I’m still on the wrong track with this and I’m working around problems that aren’t there. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:38:47+00:00Added an answer on May 28, 2026 at 4:38 am

    I’m not convinced that this extra layer of indirection adds any value. IPrincipal is already a polymorphic type, so you could accomplish exactly what you want in a much easier way.

    Instead of AuthorizeRoleContext you could just use the IPrincipal provided by Windows Authentication (WindowsPrincipal).

    Instead of AuthorizeContextAllowAll you could just implement IPrincipal like this:

    public AllowAllPrincipal : IPrincipal
    {
        public bool IsInRole(string role)
        {
            return true;
        }
    
        // also implement the Identity property...
    }
    

    Instead of AuthorizeContextAllowNothing implement IPrincipal like this:

    public AllowNothingPrincipal : IPrincipal
    {
        public bool IsInRole(string role)
        {
            return false;
        }
    
        // also implement the Identity property...
    }
    

    To configure it, you could add this to Global.asax.cs:

    private void OnAuthenticateRequest(object sender, EventArgs e)
    {
        this.Context.User = this.container.Resolve<IPrincipal>();
    }
    

    To configure Unity with the ‘real’ IPrincipal, you can set it up like this:

    this.container.RegisterType<IPrincipal>(
        new InjectionFactory(
            _ => HttpContext.Current.User));
    

    To configure Unity with the AllowAllPrincipal is much easier:

    this.container.RegisterType<IPrincipal, AllowAllPrincipal>();
    

    and likewise with AllowNothingPrincipal:

    this.container.RegisterType<IPrincipal, AllowNothingPrincipal>();
    

    If you must insist on wiring up your own AuthorizeContext, you could do so in a similar fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.