如何在中使用1.2 TLS版本.NET进行身份验证



首先,我是C#开发的新手,并试图将身份验证从基本身份验证切换到基于oauth的身份验证。然而,在测试下面的代码时,我得到了折旧异常。

#实施

// Using Microsoft.Identity.Client
var cca = ConfidentialClientApplicationBuilder
.Create(clientId)      //client Id
.WithClientSecret(clientSecret)
.WithTenantId(tenantId)
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
try
{
// Get token
var authResult = await cca.AcquireTokenForClient(ewsScopes)
.ExecuteAsync();
this.token = authResult.AccessToken;
}
catch (MsalException ex)
{
Console.WriteLine($"Error acquiring access token: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}

#异常


Microsoft.Identity.Client.MsalServiceException
HResult=0x80131500
Message=AADSTS1002016: You are using TLS version 1.0, 1.1 and/or 3DES cipher which are deprecated to improve the security posture of Azure AD. Your TenantID is: 9XXXXXXXXXXXXXXXXXXf. Please refer to https://go.microsoft.com/fwlink/?linkid=2161187 and conduct needed actions to remediate the issue. For further questions, please contact your administrator.
Trace ID: 57531a5a-2797-4f77-bc73-11b1e4355800
Correlation ID: 4295ecdd-7aa1-458f-8e6a-03fda78ec30f
Timestamp: 2022-07-25 03:32:33Z
Source=Microsoft.Identity.Client

我尝试使用ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

不幸的是,这不起作用。若有人能找到解决上述异常的方法,我们将不胜感激。

也许这会有所帮助:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

在代码启动的地方添加这一行(必须尽快到达(,不要再添加| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

它对我有效。

最新更新