Power BI:System.InvalidOperationException:当应用程序未在其中运行时,显示模式对话框或窗体

问题描述 投票:0回答:1

在IIS中部署项目时,我们面临以下错误,并且从Visual Studio运行时,它工作正常。

调用以下方法获取AccessToken时出错

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

错误是:

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() 

此处为应用类型:本机生成this.ClientID

网址: https : //dev.powerbi.com/apps

如果有人对上述错误有任何想法,请帮助我们。

asp.net-mvc azure iis powerbi powerbi-embedded
1个回答
0
投票

当我使用本机应用程序类型时,服务器端出现AAD弹出窗口,导致此问题。

然后我将其更改为“ Web应用程序类型”,并使用以下代码获取访问令牌,问题已解决。

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;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.