gpt4 book ai didi

c# - ADAL 身份验证出错

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:22 25 4
gpt4 key购买 nike

var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");


internal static async Task<OutlookServicesClient> EnsureOutlookServicesClientCreatedAsync(string capabilityName)
{
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));

try
{
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

return authResult.AccessToken;
});

var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);

return new OutlookServicesClient(dcr.ServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId,
new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
return authResult.AccessToken;
});
}
catch (AdalException exception)
{
//Handle token acquisition failure
if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
{
authContext.TokenCache.Clear();
throw exception;
}
return null;
}
public ADALTokenCache(string user)
{
// associate the cache to the current user of the web app
User = user;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
this.BeforeWrite = BeforeWriteNotification;

// look up the entry in the DB
Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
// place the entry in memory
this.Deserialize((Cache == null) ? null : Cache.cacheBits);
}

我正在使用此代码进行 ADAL 身份验证。这在我的本地 IIS 服务器上运行良好。当我在 AZURE VM 上托管相同的内容时,出现类似的错误

“无法静默获取 token 。调用方法 AcquireToken”。有人可以帮我解决这个错误吗?

Settings Helper代码如下。在 public ADALTokenCache(string user) 中,我们很好地获取了 userid,但得到了一个空缓存...原因是什么??

AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));

try
{
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,
new ClientCredential(SettingsHelper.ClientId,
SettingsHelper.ClientSecret),
new UserIdentifier(userObjectId,
UserIdentifierType.UniqueId));

return authResult.AccessToken;
});

最佳答案

确保您的权限不包含“common”。另外,请按照 http://www.cloudidentity.com/blog/2015/08/07/adal-diagnostics/ 中的说明打开诊断程序。并查看痕迹。通常,这是由于缓存中的不匹配 - acquiretokensilent 仅适用于缓存的 token ,并且如果您没有为缓存做种/您没有针对您之前选择的缓存实例/您传递了不同的用户标识符/您通过作为常见的权威,你会得到一个缓存未命中。

关于c# - ADAL 身份验证出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36472513/

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