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 879281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:54:23+00:00 2026-05-15T11:54:23+00:00

I need to create Unit Tests for an ASP.NET MVC 2.0 web site. The

  • 0

I need to create Unit Tests for an ASP.NET MVC 2.0 web site. The site uses Windows Authentication.

I’ve been reading up on the necessity to mock the HTTP context for code that deals with the HttpContext. I feel like I’m starting to get a handle on the DI pattern as well. (Give the class an attribute of type IRepository and then pass in a Repository object when you instantiate the controller.)

What I don’t understand, however, is the proper way to Mock the Windows Principal object available through User.Identity. Is this part of the HttpContext?

Does any body have a link to an article that demonstrates this (or a recommendation for a book)?

Thanks,

Trey Carroll

  • 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-15T11:54:23+00:00Added an answer on May 15, 2026 at 11:54 am

    I’ve used IoC to abstract this away with some success. I first defined a class to represent the currently logged in user:

    public class CurrentUser
    {
        public CurrentUser(IIdentity identity)
        {
            IsAuthenticated = identity.IsAuthenticated;
            DisplayName = identity.Name;
    
            var formsIdentity = identity as FormsIdentity;
    
            if (formsIdentity != null)
            {
                UserID = int.Parse(formsIdentity.Ticket.UserData);
            }
        }
    
        public string DisplayName { get; private set; }
        public bool IsAuthenticated { get; private set; }
        public int UserID { get; private set; }
    }
    

    It takes an IIdentity in the constructor to set its values. For unit tests, you could add another constructor to allow you bypass the IIdentity dependency.

    And then I use Ninject (pick your favorite IoC container, doesn’t matter), and created a binding for IIdentity as such:

    Bind<IIdentity>().ToMethod(c => HttpContext.Current.User.Identity);
    

    Then, inside of my controller I declare the dependency in the constructor:

    CurrentUser _currentUser;
    
    public HomeController(CurrentUser currentUser)
    {
        _currentUser = currentUser;
    }
    

    The IoC container sees that HomeController takes a CurrentUser object, and the CurrentUser constructor takes an IIdentity. It will resolve the dependencies automatically, and voila! Your controller can know who the currently logged on user is. It seems to work pretty well for me with FormsAuthentication. You might be able to adapt this example to Windows Authentication.

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

Sidebar

Related Questions

I decided to add unit tests to my project and continue development in a
I have an application where many unit tests use a real connection to an
I've been writing tests for a while now and I'm starting to get the
i have to create a program for a kind of complex proces. Well, the
My team creates a number of dynamic/data-driven websites. We use a CruiseControl.NET to download
I don't know if I totally got the concept wrong, but I want to
I want to make sure I am testing Models/Objects in isolation and not as
Is it possible to mock the enterprise library 5 version of 'Database'? If so...
I'm starting with a new application using silverlight and the first problem I have
IMO one of the main characteristics of a good TDD is: testing your class

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.