Asp.net mvc Power BI:System.InvalidOperationException:当应用程序未在中运行时显示模式对话框或窗体
在IIS中部署项目时,我们面临以下错误,从VisualStudio运行时,它工作正常 调用下面的方法获取AccessToken时出错Asp.net mvc Power BI:System.InvalidOperationException:当应用程序未在中运行时显示模式对话框或窗体,asp.net-mvc,azure,iis,powerbi,powerbi-embedded,Asp.net Mvc,Azure,Iis,Powerbi,Powerbi Embedded,在IIS中部署项目时,我们面临以下错误,从VisualStudio运行时,它工作正常 调用下面的方法获取AccessToken时出错 return authContext.AcquireToken(this.PowerBiAPI, this.ClientID, new Uri(this.RedirectUrl)).AccessToken; 错误是: System.InvalidOperationException: Showing a modal dialog box or form whe
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:Native
网站:
如果有人对上述错误有任何想法,请帮助我们。因为我使用的是应用程序类型本机,服务器端的AAD弹出窗口会导致此问题 然后我将其更改为App Type为Web App,并使用下面的代码获取访问令牌,问题得到了解决
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;
}
}
公共异步任务GetAccessToken()
{
尝试
{
var password=Utility.Decrypt(this.PWBI_Psw);
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“授权类型”、“密码”),
新的KeyValuePair(“范围”、“openid”),
新的KeyValuePair(“用户名”,此.PWBI_用户),
新的KeyValuePair(“密码”,password),
新的KeyValuePair(“客户端id”,this.ClientID),
新的KeyValuePair(“client_secret”,this.ClientSecretKey),
新的KeyValuePair(“资源”,this.PowerBiAPI)
});
string tokenEndpointUri=string.Format(this.tokenEndpointUri,this.TenantID);
使用(var client=new HttpClient())
{
HttpResponseMessage res=client.PostAsync(tokenEndpointUri,content).Result;
string json=await res.Content.ReadAsStringAsync();
var obj=JObject.Parse(json);
var AccessToken=(字符串)obj[“access_token”];
返回AccessToken;
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
感谢您返回并发布您的解决方案。