This article describes how to set configuration values and to acquire them in the controller of mvc web app.
This article uses the code sample of the article ‘Programmable authentication flow for accessing to Microsoft Graph‘, thus please download it before reading this article.
>>The original code before modifying in this article
・Modify appsettings.json of the code sample
The code sample of the article ‘Programmable authentication flow for accessing to Microsoft Graph‘ accesses Microsoft Graph API. It has to declare a tenant id, client id, and so on when the app access to Microsoft Graph API, thus it might define at appsettings.json.
{ "MicrosoftIdentity": { "TenantID": "__YOUR TENANT ID__", "ClientId": "__YOUR CLIENT ID__", "ClientSecret": "YOUR CLIENT SECRET", "Account": "__YOUR AAD ACCOUNT__", "Password": "__YOUR AAD PASSWORD__" }, "Logging": { ... }, "Console": { ... } } }
・Modify Startup
To acquire configuration values from the appsetting.json, modify the Startup method of the Startup class of the project. Add the below code under the statement ‘Configuration = configuration’. Use variables of the Environment object as a container to pass configuration values from the Startup class to Controllers.
・Use the environment values from the controller
In the AccountController, use the environment value ‘TenantID’ and ‘ClientId’ for login to Microsoft Azure Active Directory endpoint. Then transfer to the Index action of the Home controller.
In the Index action, the endpoint returns the authorized code when a user authenticates ended, so use the environment value ‘ClientSecret’ and the code to acquire user information from Microsoft Graph API.
When acquired the token from the code as the ‘user.read’ scope, the GetToken method of the AccessGraph class acquires user information of the login user from Microsoft Azure Active Directory, then returns a true value as means sign-in is successful. When sign-in successful, get the token as the ‘file.read’ scope using the ‘password’ grant type for acquiring the web link, so use the environment values ‘Account’ and ‘Password’.
No responses yet