Skip to content

Latest commit

 

History

History
 
 

desktop-winforms

languages page_type name description products urlFragment
csharp
sample
Windows Forms app that makes a request to the Graph API after signing in the user
This .NET 6 Windows Forms app signs in the user and then makes a request to Microsoft Graph for the user's profile data.
azure
azure-active-directory
ms-graph
microsoft-identity-platform
ms-identity-docs-code-app-csharp-winforms

.NET | Windows Forms | user sign-in, protected web API access (Microsoft Graph) | Microsoft identity platform

This .NET Windows Forms application authenticates a user and then makes a request to the Graph API as the authenticated user. The response to the request is presented to the user.

A screenshot of a Windows Forms application displaying a response from Microsoft Graph.

Prerequisites

Setup

1. Register the app

First, complete the steps in Register an application with the Microsoft identity platform to register the application.

Use these settings in your app registration.

App registration
setting
Value for this sample app Notes
Name dotnet-winforms Suggested value for this sample.
You can change the app name at any time.
Supported account types Accounts in this organizational directory only (Single tenant) Suggested value for this sample.
Platform type Mobile and desktop applications Required value for this sample
Redirect URIs http://localhost Required value for this sample

ℹ️ Bold text in the tables above matches (or is similar to) a UI element in the Azure portal, while code formatting indicates a value you enter into a text box in the Azure portal.

2. Open the project in Visual Studio

Next, open the MsalExamples.csproj project in Visual Studio.

3. Update code sample in MainWindow.cs with app registration values

Finally, set the following values in MainWindow.cs.

// 'Tenant ID' of your Azure AD instance - this value is a GUID
TenantId = "",

// 'Application (client) ID' of app registration in Azure portal - this value is a GUID
ClientId = ""

Run the application

Run the application by pressing F5 in Visual Studio.

The appliction will open allowing you to click the Sign In (if needed) & Call Graph button to use the authentication flow.

A screenshot of a Windows Forms application guiding the user to click the "Sign In" button.

About the code

This .NET 6 Windows Forms application presents a button that initiates an authentication flow using the Microsoft Authentication Library (MSAL). The user completes this flow in their default web browser. Upon successful authentication, an HTTP GET request to the Microsoft Graph /me endpoint is issued with the user's access token in the HTTP header. The response from the GET request is then displayed to the user. The MSAL client first looks to its token cache, refreshing if necessary, before acquiring a new access token.

Reporting problems

Sample app not working?

If you can't get the sample working, you've checked Stack Overflow, and you've already searched the issues in this sample's repository, open an issue report the problem.

  1. Search the GitHub issues in the repository - your problem might already have been reported or have an answer.
  2. Nothing similar? Open an issue that clearly explains the problem you're having running the sample app.

All other issues

⚠️ WARNING: Any issue in this repository not limited to running one of its sample apps will be closed without being addressed.

For all other requests, see Support and help options for developers | Microsoft identity platform.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.