Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
varund29
Advocate I
Advocate I

Power BI : System.InvalidOperationException: Showing a modal dialog box or form when the application

We are facing following error when deployed project in IIS, And it is working fine when ran from visual studio.

Error at when calling below method to get AccessToken

 

return authContext.AcquireToken(this.PowerBiAPI, this.ClientID, new Uri(this.RedirectUrl)).AccessToken;

 

Error is :

 

System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application. at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.RunAsyncTask[T](Task`1 task) at DSLUI.Controllers.BusinessLogic.ReportService.GetAccessToken()

 

Here this.ClientID is generated for App Type: Native

web : https://dev.powerbi.com/apps

If anyone have any idea regarding above error, please help us.

1 ACCEPTED SOLUTION
varund29
Advocate I
Advocate I

 

As i am using App type Native,AAD popup comming at server side causing this issue.

Then i changes it to App Type to Web app, and getting access token using below code, Issue got resolved.

 

public async Task<string> GetAccessToken()
    {
        try
        {
            var password = Utility.Decrypt(this.PWBI_Psw);

            var content = new FormUrlEncodedContent(new[]
                 {
                 new KeyValuePair<string, string>("grant_type", "password"),
                 new KeyValuePair<string, string>("scope", "openid"),
                 new KeyValuePair<string, string>("username", this.PWBI_User),
                 new KeyValuePair<string, string>("password", password),
                 new KeyValuePair<string, string>("client_id", this.ClientID),
                 new KeyValuePair<string, string>("client_secret", this.ClientSecretKey),
                 new KeyValuePair<string, string>("resource", this.PowerBiAPI)
                 });

            string tokenEndpointUri = string.Format(this.TokenEndpointUri, this.TenantID);
            using (var client = new HttpClient())
            {
                HttpResponseMessage res = client.PostAsync(tokenEndpointUri, content).Result;

                string json = await res.Content.ReadAsStringAsync();

                var obj = JObject.Parse(json);
                var AccessToken = (string)obj["access_token"];

                return AccessToken;
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

View solution in original post

3 REPLIES 3
varund29
Advocate I
Advocate I

 

As i am using App type Native,AAD popup comming at server side causing this issue.

Then i changes it to App Type to Web app, and getting access token using below code, Issue got resolved.

 

public async Task<string> GetAccessToken()
    {
        try
        {
            var password = Utility.Decrypt(this.PWBI_Psw);

            var content = new FormUrlEncodedContent(new[]
                 {
                 new KeyValuePair<string, string>("grant_type", "password"),
                 new KeyValuePair<string, string>("scope", "openid"),
                 new KeyValuePair<string, string>("username", this.PWBI_User),
                 new KeyValuePair<string, string>("password", password),
                 new KeyValuePair<string, string>("client_id", this.ClientID),
                 new KeyValuePair<string, string>("client_secret", this.ClientSecretKey),
                 new KeyValuePair<string, string>("resource", this.PowerBiAPI)
                 });

            string tokenEndpointUri = string.Format(this.TokenEndpointUri, this.TenantID);
            using (var client = new HttpClient())
            {
                HttpResponseMessage res = client.PostAsync(tokenEndpointUri, content).Result;

                string json = await res.Content.ReadAsStringAsync();

                var obj = JObject.Parse(json);
                var AccessToken = (string)obj["access_token"];

                return AccessToken;
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
Vicky_Song
Impactful Individual
Impactful Individual

 
v-haibl-msft
Microsoft Employee
Microsoft Employee

@varund29

 

Please take a look at this thread to see if it helps.

 

Best Regards,
Herbert

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors