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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:29:19+00:00 2026-05-15T23:29:19+00:00

earlier today I asked this question . So since moq creates it’s own class

  • 0

earlier today I asked this question.

So since moq creates it’s own class from an interface I wasn’t able to cast it to a different class.

So it got me wondering what if I created a ICustomPrincipal and tried to cast to that.

This is how my mocks look:

var MockHttpContext = new Mock<HttpContextBase>();
var MockPrincipal = new Mock<ICustomPrincipal>();

MockHttpContext.SetupGet(h => h.User).Returns(MockPrincipal.Object);

In the method I am trying to test the follow code gives the error(again):

var user = (ICustomPrincipal)httpContext.User;

The error is the following:

Unable to cast object of type 'IPrincipalProxy4081807111564298854aabfc890edcc8' 
to type 'MyProject.Web.ICustomPrincipal'.

I guess I still need some practice with interfaces and moq but shouldn’t I be able to cast the class that moq created back to ICustomPrincipal? I know httpContext.User returns an IPrincipal so maybe something gets lost there?

Well if anybody can help me I would appreciate that.

Pickels

Edit:
As requested the full code of the method I am testing. It’s still not finished but this is what I have so far:

public bool AuthorizeCore(HttpContextBase httpContext)
{
    if (httpContext == null)
    {
        throw new ArgumentNullException("httpContext");
    }

    var user = (ICustomPrincipal)httpContext.User;

    if (!user.Identity.IsAuthenticated)
    {
        return false;
    }

    return true;
}

Edit2:

Seems that if I use Thread.CurrentPrincipal instead of HttpContext.current.user I can cast it without a problem. Reading up on the differences between the two now.

  • 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-15T23:29:20+00:00Added an answer on May 15, 2026 at 11:29 pm

    I think you need to be able to inject your mocks into your code…

    For instance, in your class if you add the following:

    public static HttpContextBase HttpContext;
    public static ICustomPrincipal User;
    

    and have the following in your code…

    var user = (ICustomPrincipal)User;
    

    and in your class under test (say it is named ClassUnderTest)

    ClassUnderTest.HttpContextBase = MockHttpContext.Object;
    

    and

    ClassUnderTest.User = MockPrincipal.Object;
    

    well… I think that that should fix things for you.

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

Sidebar

Related Questions

Earlier today I asked a question about configuring log4net from code and got an
Earlier today I asked this question . After spending some time investigating this issue,
I know I have asked this question in a different manner earlier today but
This arose from a question earlier today on the subject of bignum libraries and
...and I mean LITERALLY just beginning. As in, I just started earlier today. All
edit 10/8/10 @ 8:20am est - since I can't make this work in prod,
I have a basic query fetching results for all results earlier than today: ...where
I've been playing around with Pyramid lately and, coming from a Pylons background, I've
I am writing a script to make chunks of a text and send it
I just forked a copy of an app on github and saved it on

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.