MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.28k stars 21.47k forks source link

Action involving calling graph API works locally but throws this error when called from the published url: The Development environment shouldn't be enabled for deployed applications #105279

Open HHOSSAIN opened 1 year ago

HHOSSAIN commented 1 year ago

In my app, I have a "Test Page" button in my navbar which opens a view where I render the display name of the signed in user by making call to the graph api using the graph service client. The calls works successfully when I run the app locally but when I try to click the 'Test Page" button in my published app, I get this "The Development environment shouldn't be enabled for deployed applications. It can result in displaying sensitive information from exceptions to end users. For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development and restarting the app." error. This error message generated is based upon the message in the "Error.cshtml" file under the "shared" folder and does not show exactly what exactly is causing the error.

My web app is built using asp .net core version 6 mvc web app template. Users are able to sign in to the app from any tenant with their microsoft account and the sign in and sign out function works without issue both locally and from the published version. This should mean the access token is correct. Then why are my calls to Microsoft graph api successful when running the app locally but fails when I try to make the call using the published app? Is there any authorisation issue with graph api when we publish the app in "azurewebsites.net" or is it some issue in my code? Some help on this issue would be highly appreciated.

graphError

Here is my published web app link- https://microsoftgraphad3.azurewebsites.net/


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AjayBathini-MSFT commented 1 year ago

@HHOSSAIN It would be great if you could add a link to the documentation you are following for these steps? This would help us redirect the issue to the appropriate team. Thanks!!

HHOSSAIN commented 1 year ago

@AjayBathini-MSFT The authentication and authorization of my app was set using the template from visual studio where I selected "Authentication Type" to use Microsoft Identity platform which generated the required code for sign in and sign out. I am attaching the snippet of the generated code in program.cs. After that, I used a private readonly GraphServiceClient attribute in HomeController which I passed into the constructor of the home controller as dependency injection. In my Home Controller, I have an async function where I try to make a graph api call to get the user's DisplayName which I store in ViewData["PName"]. I have attached code of that screenshot as well. This async function just returns bool which I hard coded to be true. I have a tab in my UI to call this function and when I call this function by clicking in localhost, it returns a page where it is written true. But when I call this same function in the deployed version, I get the "Development Mode" error page as shown in the screenshot. I tried changing the "ASPNETCORE_ENVIRONMENT": "Development" in launchsettings.json to "ASPNETCORE_ENVIRONMENT": "Production" but that gives error as well. To call the graph api, I used Microsoft Graph SDK. Here is the doc link: https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS

image

image

image

image

image

image

Naveenommi-MSFT commented 1 year ago

@HHOSSAIN Thank you for bringing this to our attention. I've delegated this to content author @darrelmiller, who will review it and offer their insightful opinions.

HHOSSAIN commented 1 year ago

@Naveenommi-MSFT or @darrelmiller I also tried updating my code using this graph tutorial Github code(https://github.com/microsoftgraph/msgraph-sample-aspnet-core). After updating, the code works locally but when I publish again in azurewebsites.net, I get prompted to sign in and after signing in, it gives error saying x.azurewebsites.net is currently unable to handle this request. The reason for the error after clicking sign in seems to be because I called the graph API at the very start this time just like it was shown in here: https://github.com/microsoftgraph/msgraph-sample-aspnet-core

HHOSSAIN commented 1 year ago

@Naveenommi-MSFT is there any update on this?

Naveenommi-MSFT commented 1 year ago

Hi @HHOSSAIN Currently our Product team working on this, once it's done, we will update you.