DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Trending

  • Building a Self-Healing Docker CI Gatekeeper With BuildKit
  • Build Production-Ready AI Phone Agents: Infrastructure, Models, and Telephony
  • Why More Executors Don't Always Make Spark Faster: Understanding AQE
  • Why Operators Ignore Model Scores

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Faking Azure AD Identity in ASP.NET Core Unit Tests

Faking Azure AD Identity in ASP.NET Core Unit Tests

Unit testing ASP.NET apps that use Microsoft Azure AD usually means working with an authenticated user. Here's how to make one for your tests.

By 
Gunnar Peipman
·
Jul. 21, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

when testing asp.net core controllers in an application that uses azure ad, we usually need a current user, at least for some tests. as there is no authenticated user when unit testing, we need to create one on our own. this blog post shows how to create a claims identity for asp.net core unit tests.

to have an azure ad user available, we have to create a fake claims identity and fill it with claims that the test expects. then we create the instance of a constructor and initiate the controller and http context. the latter one contains identity.

consider the following controller action.

public iactionresult index()
{
    var name = user.identity.name;       // do something with the name
 
    return view();
}


the test like this will probably fail with an exception, as there is no code that deals with the current user identity.

[fact]
public void sampletest()
{
    var controller = new homecontroller();       controller.index();
}


let’s organize a typical claims identity with some claims to the controller.

[fact]
public void sampletest()
{
    var user = new claimsprincipal(new claimsidentity(new claim[]
    {
            new claim(claimtypes.nameidentifier, "somevaluehere"),
            new claim(claimtypes.name, "[email protected]")
            // other required and custom claims
    }));       var controller = new homecontroller();
    controller.controllercontext = new controllercontext()
    {
        httpcontext = new defaulthttpcontext { user = user }
    };       controller.index();
}


now we have a fake claims identity available, which our controller will use. when running this test in visual studio, we can see that controller has the current user now.

aspnet-core-unit-test-claims-identity

it is also possible to use factory classes for identity. in this case, we have one class for production, which returns the current identity the controller has. another class is for testing, and this class creates and returns fake identity. as i don’t see much benefit in using factories with no additional value, i suggest going with the directly created fake identity. creating the identity can be moved to some helper method in the test class if it is needed in multiple tests.

unit test ASP.NET Core ASP.NET ADS (motorcycle) azure

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook