C# 创建允许重试Windows服务的机密ClientApplication的正确方法
我正在尝试创建Windows服务,允许我代表特定用户发送电子邮件 Graph Client的最新版本允许使用C# 创建允许重试Windows服务的机密ClientApplication的正确方法,c#,microsoft-graph-api,microsoft-graph-sdks,microsoft-graph-mail,C#,Microsoft Graph Api,Microsoft Graph Sdks,Microsoft Graph Mail,我正在尝试创建Windows服务,允许我代表特定用户发送电子邮件 Graph Client的最新版本允许使用和MaxRetry指定重试次数。 不幸的是,在创建secretentialclientapplication时,没有显示“最佳实践”的好例子 目前,我使用以下代码发送电子邮件,无需登录和密码: const string clientId = "foo...99a0"; const string clientSecret = "#6A...cx#$a"; const string tenan
和MaxRetry指定重试次数。
不幸的是,在创建secretentialclientapplication
时,没有显示“最佳实践”的好例子
目前,我使用以下代码发送电子邮件,无需登录和密码:
const string clientId = "foo...99a0";
const string clientSecret = "#6A...cx#$a";
const string tenant = "1c...7";
const string azureAdInstance = "https://login.microsoftonline.com/{0}";
var authority = string.Format(CultureInfo.InvariantCulture, azureAdInstance, tenant);
string[] scopes = { "https://graph.microsoft.com/.default" };
var clientCredentials = new ClientCredential(clientSecret);
var confidentialClientApplication =
new ConfidentialClientApplication(
clientId,
authority,
"https://daemon",
clientCredentials,
null,
new TokenCache());
var graphClient =
new GraphServiceClient("https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async(requestMessage) =>
{
var result = await confidentialClientApplication
.AcquireTokenForClientAsync(scopes);
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("bearer", result.AccessToken);
}));
var recipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new Microsoft.Graph.EmailAddress
{
Address = "test@example.com"
}
}
};
var email = new Message
{
Body = new ItemBody
{
Content = "Works fine!",
ContentType = BodyType.Html,
},
Subject = "Test",
ToRecipients = recipients
};
await graphClient
.Users["sender@example.onmicrosoft.com"]
.SendMail(email, true)
.Request()
.PostAsync();
根据我的要求,我应该如何采用它?
我不熟悉图形,所以我希望避免出现错误代码。我认为您缺少的难题之一是新的身份验证提供程序库,这里有一些如何使用这些身份验证提供程序的示例
该库基于所需的OAuth2流提供一组授权提供程序。在您的情况下,应该使用ClientCredentialsProvider而不是DelegateProvider
您不需要使用PerRequestAuthProvider。这仅适用于您希望在每次调用中在不同流或不同AppID之间切换的场景。我不知道Microsoft.Graph.Auth,谢谢您提供的链接。这个库在预览中,缺少示例,但这是一个很好的起点。演示如何正确授权和创建请求的示例将完成以下回答:)Hey@Misiu我可以推荐30天的Graph博客系列。这是一套全面的文章,帮助开发人员开始使用Graph
HttpProvider httpProvider = new HttpProvider();
var graphClient = new GraphServiceClient(appOnlyProvider, httpProvider);
graphClient.PerRequestAuthProvider = () => CreateDelegatedProvider();
var me = await graphClient.Me.Request()
.WithScopes(string[] { "User.Read" }) // adds auth scopes
.WithMaxRetry(5) // specifies maximum number of retries
.WithPerRequestAuthProvider()
.GetAsync();