This project shows how to use Microsoft Graph to query OneDrive for a specific item.
To use the ASP.NET Core MVC File Picker example you need the following:
- Visual Studio 2017 with at least .NET Core 2.1 SDK installed on your development computer.
- Either a personal Microsoft account or a work or school account.
- The application ID and key from the application that you register on the App Registration Portal.
Solution | Author(s) |
---|---|
dotnet-core-file-picker | Paolo Pialorsi (MCM, MVP, PiaSys.com, @PaoloPia) |
Version | Date | Comments |
---|---|---|
1.0 | November 6th 2018 | Initial release |
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
The file picker allows you to select a single file image from a popup showing your OneDrive content (filtered by images) and display the selected image in the web page.
This app uses the Azure AD v2.0 endpoint, so you'll register it on the App Registration Portal.
-
Sign into the portal using either your personal or work or school account.
-
Choose Add an app next to 'Converged applications'.
-
Enter a name for the app, and choose Create application. (Don't check the Guided Setup box.)
a. Enter a friendly name for the application.
b. Copy the Application Id. This is the unique identifier for your app.
c. Under Application Secrets, choose Generate New Password. Copy the password from the dialog. You won't be able to access this value again after you leave this dialog.
Important: Note that in production apps you should always use certificates as your application secrets, but for this sample we will use a simple shared secret password.
d. Under Platforms, choose Add platform.
e. Choose Web.
f. Make sure the Allow Implicit Flow check box is selected, and add
https://localhost:44339/
(or the URL of your ASP.NET Core project) as a Redirect URL. This is the base callback URL for this sample.The Allow Implicit Flow option enables the hybrid flow. During authentication, this enables the app to receive both sign-in info (the id_token) and artifacts (in this case, an authorization code) that the app can use to obtain an access token.
g. Click Save.
You'll use the application ID and secret to configure the app in Visual Studio.
- Download or clone the ASP.NET Core MVC File Picker source code.
- Open the ASPNET-File-Picker.csproj project file in Visual Studio 2017.
- In Solution Explorer, open the Index.cshtml file in /Views/Home/.
a. Replace
ENTER_YOUR_APP_ID
with the application ID of your registered application. - Press F5 to build and run the sample. This will restore all NuGet package dependencies and start up the app.
- Sign in with your personal account or your work or school account and grant the requested permissions (this will happen only the first time an application is requesting some specific permission).
- Click on the Open from OneDrive button and in the popup select the image to be displayed.
The following files contain code that's related to connecting to Microsoft Graph, open OneDrive and load a specific image file.
HomeController.cs
Handles the request from the UI.
Index.cshtml
Contains the sample's UI to open the popup that allows the image selection.