我想用MSAL库进行认证。在函数调用中,"对象父 "的作用是什么?我发现最好的是它应该是父窗口。我试过在函数调用中加入 this
(笨,我知道)而且我已经试过了。FindWindow(null, "AppName")
(从user32.dll)来获取一个IntPtr到应用程序的窗口,但是当调用这个函数时,一切都会产生一个null错误。那么,应该怎么做才能让这个函数工作呢?Unity有一个特定的句柄来处理这个函数吗?
为什么文档中完全没有提到这个所需的父函数?https:/docs.microsoft.comen-usdotnetapimicrosoft.identity.client.ipublicclientapplication.acquiretokeninteractive?view=azure-dotnet
编辑:我主要关心的是 "对象父 "应该是什么。如果不知道它应该是什么,我就不知道如何调用这个函数。从下图可以看出,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)。Visual Studio懂,Unity却不懂? 最后还是以VS使用的版本为准。删除我复制到Unity文件夹中的那个,卸载,重新安装,并按照函数确定REAL dll存在的位置,结果我不再需要一个 "父 "对象。并没有回答 "父 "是什么的问题,但现在这已经是个问题了,因为我使用了错误的dll。它也没有解决我的null异常问题,但现在这将是另一个问题。