在 Unity3D 中使用 MSAL .NET 库时未显示窗口提示



我正在尝试通过 MSAL 库(Microsoft标识平台(使用我的 Unity 应用程序登录到 SharePoint。当我第一次运行它时,我根本没有看到弹出窗口,也没有错误。(注意,我已经在编辑器中尝试过并作为独立的exe(

但是,我确实注意到第二次运行后,我得到了一个名为Oops的弹出窗口,其中包含消息Could not register the window class win32 error 0。 经过一些谷歌搜索,我认为这可能是因为在第一次尝试后背景中的某个地方有某种悬空的窗口,我需要以某种方式清理。如果我重新启动Unity,我可以再次运行,第一次没有错误,但再次没有弹出窗口。当然,如果我第二次运行,我会再次收到Oops消息。

可能不相关但值得注意的是,如果我在Oops窗口中点击"确定",我会收到一个空异常错误。似乎AcquireTokenInteractive正在尝试打开一个失败的窗口,使其为空,然后尝试从空中获取令牌。

任何有助于找出窗口不显示的原因的建议将不胜感激

using Microsoft.Identity.Client;
public class MyApp: MonoBehaviour
{
private string ClientId = "i-am-not-telling";
private string Tenant = "common";
void Start()
{
SharepointLogin();
}
public async void SharepointLogin()
{
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
.Build();
IEnumerable<string> scopes = new List<string> { "User.Read" };
AuthenticationResult result;
result = await PublicClientApp.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}
}

您是否尝试过将父窗口提供给构建器?在 Unity 中,获取窗口句柄有点困难,但此代码可能会起作用,并且可能会解决问题。

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetActiveWindow();
public static System.IntPtr GetWindowHandle() {
return GetActiveWindow();
}

然后用WithParentActivityOrWindow调用它

public async void SharepointLogin()
{
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
.WithParentActivityOrWindow(GetWindowHandle)
.Build();
IEnumerable<string> scopes = new List<string> { "User.Read" };
AuthenticationResult result;
result = await PublicClientApp.AcquireTokenInteractive(scopes)
.ExecuteAsync();
}

我相信这是 Unity 或 MSAL 库之间的错误。我将向两者提交错误报告并等待修复。这种方法暂时不起作用。我将不得不使用不同的方法来进行身份验证。

从 Unity 更新:问题来自我们在 Unity 中不支持的 System.Windows.Forms。在可预见的未来,这种行为似乎不会改变。在论坛上,我读到将System.Windows.Forms.dll添加到插件文件夹帮助了一些用户,但MSAL似乎并非如此。即使它有效,我们也不会对任何崩溃或错误负责。

我的更新:我完全取消了这种方法,并使用了这里的代码。到目前为止,它只在"本地"SharePoint网站上成功,我正在尝试获得与"SharePoint Online"服务器合作的支持。如果您没有我可能面临的相同代理问题,则可能会为其他人工作。

我发现需要确保在后台线程中调用交互式浏览器调用。 然后它按预期工作。

即。

Task.Factory.StartNew(() =>await app.AcquireTokenInteractive(STORAGE_SCOPES).ExecuteAsync());

最新更新