![](/img/trans.png)
[英]Is there a way to delete a message with Microsoft-Graph C# client library?
[英]Persist sign-in Microsoft-Graph c# SDK
我正在使用Microsoft Graph C#.NET SDK访问用户的邮件收件箱。 问题是,当我进行身份验证时,Microsoft发送给我的令牌仅在1小时左右有效,并且它会过早到期。 但是,用户每隔1小时登录才能查看Outlook邮件收件箱是非常烦人的。 我需要让这个登录PERSISTENT。
这是我使用的代码:
public static async Task Run()
{
string secret = "MyDamnPrivateSecret";
PublicClientApplication clientApp = new PublicClientApplication(secret);
GraphServiceClient graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", await GetTokenAsync(clientApp));
}));
//Processing mailbox ...
}
private static async Task<string> GetTokenAsync(PublicClientApplication clientApp)
{
if (string.IsNullOrEmpty(Properties.Settings.Default.token) || string.IsNullOrWhiteSpace(Properties.Settings.Default.token))
{
//need to pass scope of activity to get token
string[] Scopes = { "User.Read", "Mail.ReadWrite" };
string token = null;
AuthenticationResult authResult = await clientApp.AcquireTokenAsync(Scopes);
token = authResult.AccessToken;
Properties.Settings.Default.token = token;
Properties.Settings.Default.Save();
return token;
}
else
{
return Properties.Settings.Default.token;
}
}
有没有办法让过期时间更长? 或者刷新令牌或其他东西来坚持登录?
您需要请求offline_access
范围以获取刷新令牌。 如果您使用的是旧版本的MSAL,则需要在PublicClientApplication
构造函数中实现并传递令牌缓存 ,我认为MSAL将使用它来自动刷新访问令牌。 我认为较新的版本会为您处理tokenCache。
从文档中 ,这是推荐的调用模式:首先尝试调用AcquireTokenSilentAsync,如果失败并出现MsalUiRequiredException,则调用AcquireTokenAsync。
private static async Task<string> GetTokenAsync(PublicClientApplication clientApp)
{
AuthenticationResult result = null;
try
{
string[] scopes = { "User.Read", "Mail.ReadWrite", "offline_access" };
// Get the token from the cache.
result = await app.AcquireTokenSilentAsync(scopes, clientApp.Users.FirstOrDefault());
return result.AccessToken;
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync.
// This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
// Dialog opens for user.
result = await app.AcquireTokenAsync(scopes);
return result.AccessToken;
}
catch (MsalException msalex)
{
ResultText.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
}
}
catch (Exception ex)
{
ResultText.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
return;
}
}
这是一个供参考的样本。 https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2
在您的代码中, AcquireTokenAsync
始终触发登录。
相反,您需要实现令牌缓存并使用AcquireTokenSilentAsync
。
有关详细信息,请查看以下链接:
我将尝试澄清这里的问题:
MSAL .net适用于不同的平台 - .net桌面,.net核心,UWP,xamarin android和xamarin iOS。 在其中一些平台(UWP和xamarin)上,我们会为您保留令牌缓存。 在其他方面,我们希望您保留缓存。 原因是我们无法提供适用于所有场景(例如ASP.NET服务器场)的令牌序列化逻辑,因此我们希望您这样做。 我们提供样品和指导。 MSAL维基上的详细信息和一些参考实现:
@Michael提供的示例代码适用于MSAL v1。 在MSAL v2中,事情有点不同,你可以在MSAL维基上找到调用的模式:
我们请求并存储刷新令牌(RT)。 如果身份验证令牌(AT)过期,我们将根据RT请求新的令牌 - 这将在没有用户交互的情况下发生。 这应该对你都是透明的,即它应该只是工作:)。 确保您的令牌缓存序列化有效,即您在执行时获得一个帐户
// perform an interactive login first
// otherwise there will be no AT / RT in the store
var accounts = await app.GetAccountsAsync();
// there should be an account that you can use
另请查看@Daniel Dobalian对AT和RT默认到期的答复: MSAL令牌在1小时后到期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.