- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试访问 Microsoft Graph API 以获取用户的 Outlook 组。
以下是检索访问 token 的代码:
public static async Task<string> GetGraphAccessTokenAsync()
{
string AzureAdGraphResourceURL = "https://graph.microsoft.com/";
string signedInUserUniqueName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
var clientCredential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey);
var userIdentifier = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);
AuthenticationContext authContext = new AuthenticationContext(
SettingsHelper.Authority, new ADALTokenCache(signedInUserUniqueName));
var result = await authContext.AcquireTokenSilentAsync(AzureAdGraphResourceURL, clientCredential, userIdentifier);
return result.AccessToken;
}
该方法使用设置助手,如下所示:
public class SettingsHelper
{
private static string _clientId = ConfigurationManager.AppSettings["ida:ClientID"];
private static string _appKey = ConfigurationManager.AppSettings["ida:Password"];
private static string _tenantId = ConfigurationManager.AppSettings["ida:TenantID"];
private static string _authorizationUri = "https://login.windows.net";
private static string _authority = "https://login.windows.net/{0}/";
private static string _graphResourceId = "https://graph.windows.net";
public static string ClientId
{
get
{
return _clientId;
}
}
public static string AppKey
{
get
{
return _appKey;
}
}
public static string TenantId
{
get
{
return _tenantId;
}
}
public static string AuthorizationUri
{
get
{
return _authorizationUri;
}
}
public static string Authority
{
get
{
return String.Format(_authority, _tenantId);
}
}
public static string AADGraphResourceId
{
get
{
return _graphResourceId;
}
}
}
这是我收到的错误:静默获取 token 失败。调用AcquireToken方法
异常详细信息:
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException : Failed to acquire token silently. Call method AcquireToken
错误专门发生在这一行:
var result = await authContext.AcquireTokenSilentAsync(AzureAdGraphResourceURL, clientCredential, userIdentifier);
我已检查以确保 UserIdentifier 与缓存中的值匹配,但它似乎仍然拒绝 token 。关于我可能出错的地方有什么想法吗?
最佳答案
首先,请确保使用 Microsoft Graph 端点(实际上您使用的是 Active Directory 端点)
private static readonly string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static readonly string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
private static readonly string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static readonly string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
private static readonly string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
private static readonly string graphResourceId = "https://graph.microsoft.com";
private static readonly Uri graphEndpointId = new Uri("https://graph.microsoft.com/v1.0/");
在进行静默调用之前,您必须通过检索代码来进行经典调用。我假设您正在使用 MVC 应用程序。这是我的 Startup.Auth.cs 代码:
public void ConfigureAuth(IAppBuilder app)
{
ApplicationDbContext db = new ApplicationDbContext();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = AuthenticationHelper.ClientId,
Authority = AuthenticationHelper.AadInstance + AuthenticationHelper.TenantId,
PostLogoutRedirectUri = AuthenticationHelper.PostLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived =async (context) =>
{
var code = context.Code;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
try
{
var result = await AuthenticationHelper.GetAccessTokenByCodeAsync(signedInUserID, code);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
//throw;
}
}
}
});
}
这是我在 AuthenticationHelper 类中使用的代码:
public async static Task<AuthenticationResult> GetAccessTokenByCodeAsync(string signedInUserID, string code)
{
ClientCredential credential = new ClientCredential(clientId, appKey);
AuthenticationContext authContext = new AuthenticationContext(AadInstance + TenantId, new ADALTokenCache(signedInUserID));
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
return result;
}
然后,每次我需要向图表发出请求时,这里是我用来获取 token 的代码:
public async static Task<string> GetTokenForApplicationAsync()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext(AadInstance + TenantId, new ADALTokenCache(signedInUserID));
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(GraphResourceId, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
return authenticationResult.AccessToken;
}
其他需要注意的事情:确保您的tenantId是您的租户的guid。由于某种原因,有时,如果您使用租户名称,adal 会产生影响,并可能引发此类错误。
关于c# - 无法静默获取 token - Microsoft Graph API 获取用户的 Outlook 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081379/
我对 JavaScript/jQuery 和 Web 开发有点陌生。我当前的项目是为网站建立一个小型聊天室。一切正常,但我有一个问题: 如何将数据从客户端传输到服务器并返回(例如通过 javascri
warn 4 与 print STDERR 4 有何不同? perl -e 'local *STDERR; warn 4' (输出仍然转到 STDERR ) perl -e 'local *STDER
所以在面对可怕的javax.faces.application.ViewExpiredException之后,我不得不在互联网上寻找合适的解决方案。幸运的是,这些解决方案随时可用,我继续采用了 Omn
我正在尝试使用 Spring Modules 项目中的声明性缓存。 它不起作用,即。似乎没有任何东西被缓存。 这是我的配置:
我正在实现以下教程:Speech To Text 我正在使用 AVAudioEngine 和 SFSpeechRecognizer 录制音频实现语音到文本。此处教程使用以下方法将语音引导至文本一个开始
我有一个文本(有多行),需要将其放入 wxPython TextCtrl 中。 问题是其中一些行包含无法打印的字符。 我的解决方案是使用 AppendText,并 try catch 有问题的行。 当
有没有办法直接从 Angular/客户端应用程序使用“用户名”和“密码”登录? 例如像这样的东西? Keycloak.init({username: 'guest', password: 'abc'}
我这样执行 GET 请求: http://www.smurf.com/path/?var=val 这显示在用户的 URL 中。我当然在几乎每个主要网站上都看到了这一点。 但我只是好奇是否有办法绕过它。
假设我有两个相互硬链接(hard link)的文件: -rw-rw-r-- 2 mparrott grp 5 Jul 28 09:38 bar -rw-rw-r-- 2 mparrott grp 5
我想在 android 4+ 中以编程方式(直接)添加日历事件。这可以在模拟器上测试吗?我没有安卓手机。一些示例代码将不胜感激。我阅读了 android 开发人员的日历提供程序,但我很困惑。如何将事件
我正在向我的工作簿添加一个新工作表 Application.ScreenUpdating = False SheetExists = False For Each WS In Worksheets
我正在 Excel 中的 VBA 中进行一些网页解析;我是 MSHTML 来下载和解析网页。但是,在某些站点上,会弹出一个对话框。它工作正常;但是,我想知道是否有办法禁用它或可能绕过它。感谢您的时间。
有没有办法使用谷歌的 firebase 发送一个无声的 APNS? 似乎如果应用程序在后台,它总是会向用户显示通知。 谢谢? 最佳答案 您可以使用 FCM 服务器 API 发送静默 APNS 消息 h
我有一个 Powershell 脚本来放松机器上的执行策略,本质上是运行: Set-ExecutionPolicy Unrestricted -Force 由于 ExecutionPolicy 在机器
我有一个 Powershell 脚本,用于将我们的生产数据库复制到我们的测试环境以及相关的清理事件。我想清理为用户输出到控制台的信息,但在清理从 Az.sql 命令中收到的错误消息时遇到了问题。 我的
这是我当前的 Facebook 应用程序登录流程(有关解释,请参阅 this answer)。 logout() 和 login() 是在用户注销或登录时呈现页面的虚拟函数。 window.fbAsy
我有一个 React Native 应用程序,我正在尝试将静默的 iOS 推送通知发送到 JavaScript 中的处理程序。 我看到的行为是 AppDelegate 中的 didReceiveRem
我正在使用数据通知来更新我的应用程序状态,它在 Android 上运行完美,但我无法在 IOS 上接收到任何数据消息,甚至没有触发 onMessage 监听器。 我正在使用这些: firebase_c
我知道较新的 GCC 版本引入了可能错误的字符串操作“stringop-truncation”的警告 这是我可以轻松触发此警告的示例代码: $ cat strncpy-warning.cxx #inc
标题基本上描述了它。 我检查过的事情: 验证了远程 Powershell session 中的用户名(以确保事情正在以我期望的权限执行) 检查了所描述文件的权限。预期用户拥有文件的完全控制权 删除了
我是一名优秀的程序员,十分优秀!