page_type | urlFragment | products | languages | extensions | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
office-add-in-sso-aspnet |
|
|
|
This sample implements an Office Add-in that uses the getAccessToken
API in Office.js to give the add-in access to Microsoft Graph data. This sample is built on ASP.NET and Microsoft Authentication Library (MSAL) .NET.
There are two versions of the sample in this repo, one of which has its own README file:
- Begin folder. The starting point for the SSO walkthrough at Create an ASP.NET Office Add-in that uses single sign-on. Please follow the instructions in the article.
- Complete folder. The completed sample. To use this version, follow the instructions in the article Create an ASP.NET Office Add-in that uses single sign-on, but substitute "Complete" for "Begin" in those instructions and skip the sections Code the client-side and Code the server-side.
Integrating data from online service providers increases the value and adoption of your add-ins. This code sample shows you how to connect your add-in to Microsoft Graph. Use this code sample to:
- See how to use the Single Sign-on (SSO) API.
- Access Microsoft Graph on behalf of an Office Add-in.
- Build an add-in using ASP.NET Core, MSAL 4.x.x for .NET, and Office.js.
- Use the MSAL.NET Library to implement the OAuth 2.0 authorization framework in an add-in.
- Use the OneDrive REST APIs from Microsoft Graph.
- Any platform and Office host combination that supports the IdentityAPI 1.3 requirement set.
To run this code sample, the following are required:
- Visual Studio 2019 or later.
- Office Developer Tools.
- Microsoft 365 - You can get a free developer sandbox that provides a renewable 90-day Microsoft 365 E5 developer subscription.
- At least a few files and folders stored on OneDrive for Business in your Microsoft 365 subscription.
- A Microsoft Azure Tenant. This add-in requires Azure Active Directory (AD). Azure AD provides identity services that applications use for authentication and authorization. A trial subscription can be acquired here: Microsoft Azure.
Solution | Author(s) |
---|---|
Office Add-in Microsoft Graph ASP.NET | Microsoft |
Version | Date | Comments |
---|---|---|
1.0 | May 10, 2017 | Initial release |
1.0 | September 15, 2017 | Added support for 2FA. |
1.0 | December 8, 2017 | Added extensive error handling. |
1.0 | January 7, 2019 | Added information about web application security practices. |
2.0 | November 5, 2019 | Added Display Dialog API fall back and use new version of SSO API. |
2.1 | August 11, 2020 | Removed preview note because the APIs have released. |
2.2 | June 15, 2021 | Updated NuGet packages and adjust code for breaking changes. |
3.0 | November 7, 2022 | Updated to use ASP.NET Core. Removed fallback dialog approach. |
The sample sends a hardcoded query parameter on the URL for the Microsoft Graph REST API. If you modify this code in a production add-in and any part of query parameter comes from user input, be sure that it is sanitized so that it cannot be used in a Response header injection attack.
- Did you experience any problems with the sample? Create an issue and we'll help you out.
- We'd love to get your feedback about this sample. Go to our Office samples survey to give feedback and suggest improvements.
- For general questions about developing Office Add-ins, go to Microsoft Q&A using the office-js-dev tag.
Get a free sandbox, tools, and other resources you need to build solutions for the Microsoft 365 platform.
- Free developer sandbox Get a free, renewable 90-day Microsoft 365 E5 developer subscription.
- Sample data packs Automatically configure your sandbox by installing user data and content to help you build your solutions.
- Access to experts Access community events to learn from Microsoft 365 experts.
- Personalized recommendations Find developer resources quickly from your personalized dashboard.
Copyright (c) 2019 - 2021 Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Note: The Index.cshtml file contains an image URL that tracks diagnostic data for this sample add-in. Please remove the image tag if you reuse this sample in your own code project.