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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:45:09+00:00 2026-06-13T21:45:09+00:00

we have a WPF application that uses CurrentPrincipal to handle security. This all works

  • 0

we have a WPF application that uses CurrentPrincipal to handle security. This all works fine.

Now, from within a test project, we call view model operations. The problem is, that in the initialization of the test we create a new instance of the application but we don’t know how to set the CurrentPrincipal on this application:

 private void TestInitialize()
 {
      var app = new App();
      // setting Thread.CurrentPrincipal doesn't set app thread it seems
 }

Anyone has an idea how to set the CurrentPrincipal for a WPF application from outside the application?

  • 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-13T21:45:10+00:00Added an answer on June 13, 2026 at 9:45 pm

    There are many different strategies to choose here, but one strategy which many adopt to is mocking the IPrincipal and IIdentity objects necessary to simulate principals and identities of the current thread. I will show how this can be done using the Mocking framework Moq next:

    using System.Security.Principal;
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    
    namespace MockingIdentitySample.Test
    {
     [TestClass]
     public class UnitTest1
    {
    
        [TestInitialize]
        public void TestInitialize()
        {
            SetupMockedPrincipalAndIdentity(true, true, "John Doe"); 
        }
    
        private void SetupMockedPrincipalAndIdentity(bool autoSetPrincipal, bool isAuthenticated, string identityName)
        {
            var mockedPrincipal = new Mock<IPrincipal>();
            var mockedIdentity = new Mock<IIdentity>();
            mockedIdentity.Setup(m => m.Name).Returns(identityName);
            mockedIdentity.Setup(m => m.IsAuthenticated).Returns(isAuthenticated); 
            mockedPrincipal.Setup(p => p.Identity).Returns(mockedIdentity.Object);
    
            if (autoSetPrincipal)
             Thread.CurrentPrincipal = mockedPrincipal.Object;
        }
    
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual(Thread.CurrentPrincipal.Identity.Name, "John Doe");
            Assert.IsTrue(Thread.CurrentPrincipal.Identity.IsAuthenticated); 
        }
    
       }
      }
    

    Note that Thread.CurrentPrincipal can be set directly, but using a mocking framework or isolation framework in general is strongly suggested when writing unit tests. Often we are testing not the security code but are writing unit tests for view models or similar classes in our code and therefore we abstract, isolate or mock these dependencies such as the current principal on the thread away so we can focus on what we really want to write unit tests for – the logic of our code.

    Of course, one must choose if mocking is the right choice here or if Moq is the correct framework to use. There are several alternatives when it comes to mocking. Moq is really nice mocking framework, but cannot mock static methods for example. Other mocking frameworks offer more functionality than Moq and so on.

    A quickstart for Moq is available here if this seems like a possible route to follow:

    http://code.google.com/p/moq/wiki/QuickStart

    (Please note, I am an independent developer and the reason I suggested Moq was not because of my liking, but because I have used this mocking framework earlier for similar scenarios as yours here).

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

Sidebar

Related Questions

I have a simple WPF application that uses ClickOnce to handle installing. Within this
I have an application that uses WPF Composite, and I have an issue. I've
I have a fairly large application that uses WPF for its user interface. I
We have a WPF Application that runs from both desktop and as a XBAP
I have an WPF Full Tust explorer application that uses a COM assembly. In
I have a C# WPF (.NET 4.0) application that uses Excel interop to read
I have a WPF application that uses Caliburn.Micro. I have a DataGrid: <DataGrid x:Name=WeatherStations
I have a WPF application implemented using the MVVM framework that uses an ActiveX
I have an application that uses treeview in wpf with mvvm. The treeitem template
I have a WPF application that uses MVVM data bindings. I am adding items

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.