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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Infrastructure as Code (IaC) Beyond the Basics
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  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 user avatar
Gunnar Peipman
·
Jul. 21, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

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, "gunnar@somecompany.com")
            // 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, DZone MVB. 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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!