[英]Microsoft EWS throws "403 Forbidden" in modern oAuth implementation c# - Reading mail from outlook
我們已經實現了現代 OAuth 實現來讀取來自 outlook 郵箱的郵件。 我們使用的代碼是微軟官方網站上可用的任何代碼。 但它拋出“請求失敗。遠程服務器返回錯誤:(403)禁止訪問。”。 但是身份驗證令牌正在正確獲取。 這是我的代碼
static void Main(string[] args)
{
MainTask().Wait();
}
static async System.Threading.Tasks.Task MainTask()
{
// Using Microsoft.Identity.Client 4.22.0
var cca = ConfidentialClientApplicationBuilder
.Create(ConfigurationManager.AppSettings["appId"])
.WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
.WithTenantId(ConfigurationManager.AppSettings["tenantId"])
.Build();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
try
{
var authResult = await cca.AcquireTokenForClient(ewsScopes)
.ExecuteAsync();
// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "myname@mydomain.com");
//Include x-anchormailbox header
ewsClient.HttpHeaders.Add("X-AnchorMailbox", "myname@mydomain.com");
// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.Inbox, new FolderView(10));
foreach (var folder in folders)
{
Console.WriteLine($"Folder: {folder.DisplayName}");
}
}
catch (MsalException ex)
{
Console.WriteLine($"Error acquiring access token: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("Hit any key to exit...");
Console.ReadKey();
}
}
我們幾乎嘗試了所有可能的方法。 請任何已經成功實現此現代 OAuth 功能的人提供支持。 提前致謝
檢查一些文檔以了解403 (Forbidden)
的定義,將揭示它與401 (Unauthorised)
的主要區別,即403
與用戶沒有足夠的權限訪問資源有關。 這意味着,雖然您可能擁有有效的身份驗證令牌,但該令牌不會授予您訪問所請求的特定資源的權限。 您應該檢查您正在驗證的用戶的權限。
嘗試在 AAD 上授予“full_access_as_app”,您必須授予管理員權限,然后嘗試執行代碼。
查看下一篇文章,看看是否有幫助: 錯誤 403:當我嘗試連接到服務器 OAuth 2.0 時被禁止
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.