gpt4 book ai didi

c# - AuthenticationContext.AcquireTokenAsync

转载 作者:太空狗 更新时间:2023-10-30 00:30:07 24 4
gpt4 key购买 nike

我希望能够以编程方式从 Azure 获取 token 。

我调用 GetAToken().Wait(); 但失败了。

方法是:

public async Task<string> GetAToken()
{
// authentication parameters
string clientID = "*********";
string username = "<azure login>";
string password = "<azure login password>";
string directoryName = "<AD Domain name>";

ClientCredential cc = new ClientCredential(clientID, password);
var authenticationContext = new AuthenticationContext(
"https://login.windows.net/" + directoryName);

AuthenticationResult result = await authenticationContext.AcquireTokenAsync(
"https://management.core.windows.net/", cc);

if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}

string token = result.AccessToken;

return token;
}

最佳答案

所以不确定您是在 Android、iOS 还是 Xamarin.Forms 上执行此操作。以下是我将如何使用 ADAL 和 Azure 进行身份验证(代码在我这边运行):

在 Android 上:

public async Task<AuthenticationResult> Authenticate(Activity context, string authority, string resource, string clientId, string returnUri)
{
var authContext = new AuthenticationContext(authority);
if (authContext.TokenCache.ReadItems().Any())
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

var uri = new Uri(returnUri);
var platformParams = new PlatformParameters(context);
try
{
var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
return authResult;
}
catch (AdalException e)
{
return null;
}
}

在 iOS 上:

public async Task<AuthenticationResult> Authenticate(UIViewController controller, string authority, string resource, string clientId, string returnUri)
{
var authContext = new AuthenticationContext(authority);
if (authContext.TokenCache.ReadItems().Any())
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

var controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
var uri = new Uri(returnUri);
var platformParams = new PlatformParameters(controller);

try
{
var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
return authResult;
}
catch (AdalException e)
{
return null;
}
}

关于UWP :

public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
{
var authContext = new AuthenticationContext(authority);
if (authContext.TokenCache.ReadItems().Any())
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

var uri = new Uri(returnUri);
var platformParams = new PlatformParameters(PromptBehavior.Auto);
try
{
var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
return authResult;
}
catch (AdalException e)
{
return null;
}
}

我传递给上述方法的变量:

string authority = "https://login.windows.net/common";
string ResourceID = "Backend ClientId";//Backend (web app)
string clientId = "Native App ClientId";//native app
string returnUri = "https://{My Azure Site}.azurewebsites.net/.auth/login/done";

如果您想在 Xamarin.Forms 中执行此操作,下面是我的 GitHub 解决方案的链接,我在其中通过 DependencyService 公开了这些方法。

我希望这有帮助!如果您的响应中出现任何错误,请检查以确保您在 Azure 中正确设置了权限。我这样做就像 this 。另一个很棒的资源是Adrian Hall's Xamarin/Azure book

编辑:添加了 UWP 内容

关于c# - AuthenticationContext.AcquireTokenAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com