从 Unity 应用程序中调用时,在 MSAL 的 AcquireTokenInteractive 中为 Parent Object 添加什么?
When Calling from within a Unity App, What to Put for Parent Object in MSAL's AcquireTokenInteractive?
我正在尝试使用 MSAL 库进行身份验证。 "object parent" 在函数调用中的目的是什么?我发现最好的应该是 parent Window。我试过放置 this
(愚蠢,我知道)并且我试过 FindWindow(null, "AppName")
(来自 user32.dll)以获取应用程序 window 的 IntPtr,但是调用函数时,一切都会产生空错误。那么应该去那里做什么才能使这个功能起作用呢? Unity 是否有具体的处理方法?
为什么文档根本不提到这个要求 parent?
https://docs.microsoft.com/en-us/dotnet/api/microsoft.identity.client.ipublicclientapplication.acquiretokeninteractive?view=azure-dotnet
编辑:我主要关心 "object parent" 应该是什么。不知道它应该是什么,我不知道如何调用该函数。如下图所示,Visual Studio 在没有它的情况下标记调用并强迫我将其放入。我现在包括其余代码,以防有人希望完全指出我可能正在做的其他事情除了有关使用 "parent".
的问题外,还有其他错误
using Microsoft.Identity.Client;
public async void SharepointLogin()
{
IntPtr windowPtr = FindWindow(null, "MyAppName"); // This is the "parent" object that I have no idea what it's supposed to be as it is not documented.
IPublicClientApplication PublicClientApp;
Debug.Log("Creating PublicClientApplicationBuilder");
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
.Build();
Debug.Log("Getting Accounts");
var accounts = await PublicClientApp.GetAccountsAsync();
IEnumerable<string> scopes = new List<string> { "User.Read" };
AuthenticationResult result;
try
{
Debug.Log("Trying Silent Token");
result = await PublicClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
Debug.Log("Failed. Trying Interactive Token");
// This is the line where I can't remove the "parent" object
// (in this case "windowPtr") without the error seen in the image
// below. It has no compile problems like this but it always has a
// null pointer exception when it runs. Everything is the same in the
// first "Try" except this "parent" object I'm forced to supply.
result = await PublicClientApp.AcquireTokenInteractive(scopes, windowPtr)
.ExecuteAsync();
}
}
@Skyler Lauren 走在正确的轨道上。虽然 Visual Studio 声称我安装了 4.14.0,但无论我从他回答 时复制的 dll 版本 (3.0) 最终胜过 VS 使用的版本。删除我复制到 Unity 文件夹中的那个,卸载,重新安装,并按照函数确定 REAL dll 存在的位置导致我不再需要 "parent" 对象。没有回答 "parent" 最初是什么的问题,但现在这是一个有争议的问题,因为我使用了错误的 dll。它也没有解决我的 null 异常问题,但现在这将是一个不同的问题。
我正在尝试使用 MSAL 库进行身份验证。 "object parent" 在函数调用中的目的是什么?我发现最好的应该是 parent Window。我试过放置 this
(愚蠢,我知道)并且我试过 FindWindow(null, "AppName")
(来自 user32.dll)以获取应用程序 window 的 IntPtr,但是调用函数时,一切都会产生空错误。那么应该去那里做什么才能使这个功能起作用呢? Unity 是否有具体的处理方法?
为什么文档根本不提到这个要求 parent? https://docs.microsoft.com/en-us/dotnet/api/microsoft.identity.client.ipublicclientapplication.acquiretokeninteractive?view=azure-dotnet
编辑:我主要关心 "object parent" 应该是什么。不知道它应该是什么,我不知道如何调用该函数。如下图所示,Visual Studio 在没有它的情况下标记调用并强迫我将其放入。我现在包括其余代码,以防有人希望完全指出我可能正在做的其他事情除了有关使用 "parent".
的问题外,还有其他错误 using Microsoft.Identity.Client;
public async void SharepointLogin()
{
IntPtr windowPtr = FindWindow(null, "MyAppName"); // This is the "parent" object that I have no idea what it's supposed to be as it is not documented.
IPublicClientApplication PublicClientApp;
Debug.Log("Creating PublicClientApplicationBuilder");
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, Tenant)
.Build();
Debug.Log("Getting Accounts");
var accounts = await PublicClientApp.GetAccountsAsync();
IEnumerable<string> scopes = new List<string> { "User.Read" };
AuthenticationResult result;
try
{
Debug.Log("Trying Silent Token");
result = await PublicClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
Debug.Log("Failed. Trying Interactive Token");
// This is the line where I can't remove the "parent" object
// (in this case "windowPtr") without the error seen in the image
// below. It has no compile problems like this but it always has a
// null pointer exception when it runs. Everything is the same in the
// first "Try" except this "parent" object I'm forced to supply.
result = await PublicClientApp.AcquireTokenInteractive(scopes, windowPtr)
.ExecuteAsync();
}
}
@Skyler Lauren 走在正确的轨道上。虽然 Visual Studio 声称我安装了 4.14.0,但无论我从他回答