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

  • SEARCH
  • Home
  • 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 8817499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:51:51+00:00 2026-06-14T04:51:51+00:00

We are required to use Windows authentication on a new project. So we will

  • 0

We are required to use Windows authentication on a new project. So we will be utilizing the AuthorizeAttribute on action methods, and perhaps also within a limited number of controller actions.

Works great, of course. But to test this (both in unit testing, and in testing by hand as I integrate this stuff), I need to be able to simulate a user with any role, and to switch frequently back and forth between different roles.

I cannot modify the controller’s User object (it’s readonly), so from where can I insert a fake user that implements IPrincipal that to be accessed everywhere in my app, including:

  • in controller-actions
  • in custom attributes

I have not yet dived into DI frameworks — will one be necessary at this point? If you strongly recommend so, I’d still like to know how to do this via “poor-man’s” DI.

  • 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-06-14T04:51:53+00:00Added an answer on June 14, 2026 at 4:51 am

    In global.asax / Application_PostAuthenticateRequest(object sender, EventArgs e).

    That method is invoked after ASP.NET have had it’s fun with the principal.

    If you only want to provide your own roles you could just implement a RoleProvider instead.

    Update

    You can do this:

    public void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        var principal = LoadFromTheDb();
        HttpContext.Current.User = Thread.CurrentPrincipal = principal;
    }
    

    And then you can use that principal wherever you like.

    If you have enabled runAllManagedModulesForAllRequests in web.config you should probably check if the user is authenticated first before doing anything else.

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

Sidebar

Related Questions

I have a web project that used to use Forms Authentication. I have new
I am working on a project for which we are required to use transaction
We would like to use Integrated Windows Authentication if a user accesses our SharePoint
I am starting a new project using WCF so that I can use jQuery
Is it possbile to use the windows login credential for proxy authentication using C#.
We currently use SQL 2008 with Windows Authentication disabled, we only allow SQL Mixed
I want to use ASP.NET Membership Provider with Windows Authentication and Membership Roles. I
So we're required to use the following order for CSS anchor pseudo-classes a:link {
Is it possible to use [Required] attribute in model, but not in controller. For
what I want to do is to use something like name=validate[required] basically use it

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.