Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
You can use Microsoft Graph to consume user information stored in Microsoft 365 in custom applications. By using Connected Services in Visual Studio, you can grant your application access to the following Microsoft 365 services:
This article describes how to configure Connected Services in Visual Studio to use Microsoft Graph in an ASP.NET MVC application that displays events from the currently signed in user's calendar.
To use the Office 365 Connected Services with Microsoft Graph, you need to:
Download the Microsoft Graph ASP.NET Connected Services MVC App Sample. This sample includes the references that you need to authenticate against Microsoft Graph. After you download the starter project, unzip, and open the graph-tutorial sample in Visual Studio.
Enter the domain of your developer account and choose Next.
Go Microsoft Entra admin center to find your domain name. Sign in, expand the Identity menu > select Overview. The field Primary domain in Basic information section contains your domain name.
Tip: If your primary address is
admin@contoso.com
, your domain is contoso.com`.
Select Create a new application registration and complete the process of creating an app registration.
For this tutorial, select the following permissions:
Calendar.Read
permission.User.Read
permission.Choose Finish.
Double-click Web.config.
Inside <appSettings>, insert the following code:
<add key="ida:RedirectUri" value="https://localhost:PORT/" />
<add key="ida:AppScopes" value="User.Read Calendars.Read" />
Modify the PORT
value for ida:RedirectUri
to match the URL of your application.
Tip: You can find the PORT information in the project properties.
Save your changes and start the project. Next, select the Click here to sign in button that redirects you to https://login.microsoftonline.com. Sign in with your developer account and consent to the requested permissions.
The home page displays your name that indicates that you're signed-in. On the Calendar tab, a table of events displays accordingly to your account.
Select Sign Out on the avatar in the top-right corner to reset the session and return to the home page.
You can now explore the files and code in Visual Studio to learn more about this starter project.
The Helpers\GraphHelper.cs contains the methods that use the GraphServiceClient to send requests to the Microsoft Graph service. This class implements the GetUserDetailsAsync method that uses the Microsoft Graph SDK to retreive user's information by calling the /me
endpoint.
The GetEventsAsync method uses the /v1.0/me/events
endpoint to request calendars data. The select
OData query parameter limits the fields returned for each event to just those displayed in the view. The orderBy
parameter sorts the results by the date and time they were created, with the most recent item being first.
The GetAuthenticatedClient method initializes a GraphServiceClient with an authentication provider and attempts to retrieve a previously obtained access token from the token store using the AcquireTokenSilent method. Notice that if the AcquireTokenSilent fails, the user is presented with an interactive login.
The App_Start\Startup.Auth.cs configures the OWIN middleware with the values from Web.config and defines the following callback methods OnAuthenticationFailedAsync and OnAuthorizationCodeReceivedAsync that are invoked when the sign-in process returns from Azure.
The method OnAuthorizationCodeReceivedAsync wraps the default user token cache of the ConfidentialClientApplication with the SessionTokenStore class. The MSAL library handles the logic of storing the tokens and refreshing it when needed. The code passes the user details obtained from Microsoft Graph to the SessionTokenStore object to store in the session.
The TokenStorage\SessionTokenCache.cs implements a token store class to serialize and store the MSAL token cache and the user's details in the user session. You can replace this with your own custom token cache. For more information, see Cache access tokens.
The Controllers\AccountController.cs is a controller to handle sign-in that defines a SignIn and SignOut action. The SignIn action checks if the request is already authenticated. If not, it invokes the OWIN middleware to authenticate the user. The SignOut action invokes the OWIN middleware to sign out.
The Views\Shared\_Layout.cshtml defines the global layout of the app. It adds Bootstrap for simple styling and Font Awesome for icons, defines the layout of the nav bar, and uses the Alert class to display alerts.
The Views\Home\Index.cshtml and Views\Calendar\Index.cshtml contain the UI to display the information retrieved from Azure.
If you need help, post your questions on Microsoft Q&A. Tag your post with {microsoft-graph-identity}.
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Build ASP.NET MVC web applications with the Microsoft Graph .NET SDK - Training
Learn how to use the Microsoft Graph .NET SDK in an ASP.NET MVC web application.
Certification
Microsoft Certified: Identity and Access Administrator Associate - Certifications
Demonstrate the features of Microsoft Entra ID to modernize identity solutions, implement hybrid solutions, and implement identity governance.
Documentation
Use Graph Explorer to try Microsoft Graph APIs - Microsoft Graph
Try Microsoft Graph APIs on the default sample tenant to explore capabilities, or sign in to your tenant and use it as a prototyping tool to fulfill your app scenarios.
Best practices for working with Microsoft Graph - Microsoft Graph
Apply these best practices to improve your Microsoft Graph application's performance and make your app more reliable for end users.
Use the Microsoft Graph API - Microsoft Graph
Learn how to register your app and get authentication tokens for a user or service before you make requests to the Microsoft Graph API.