公司之前有一個進行郵件獲取的功能,使用ExchangeService+TLS1.2認證的方式,但是由于微軟將要將這種認證方式列為Legacy authentication,且將在2022年10月下載該認證方式。因此將嘗試使用OAuth+EWS這種認證的方式。別問問啥必須用微軟系的功能,問就是公司硬性要求。
如下為更改通知:
Stop legacy authentication:
Microsoft have announced that Exchange online – will stop allowing legacy authentication from October 2022
I am writing to you – because you are registered as owner of an account used for legacy authentication. (Legacy authentication is basically using username / password for authentication)
Reason
The reason why Microsoft ?choose to remove support is Legacy authentication is that it considered insecure and added a risk to XXXXXXXXXXXX for password spray attacks.
Information
The spreadsheet included is also published here and general information on changes in the cloud: Information on changes in Azure for developers and on-line services (sharepoint.com)
The notification from Microsoft: Basic Authentication and Exchange Online – September 2021 Update - Microsoft Tech Community
Near Future
It is planned to block Legacy Authentication for new systems – in very near future.
開始之前,先看一下下文中Delegated權限和Application權限的區(qū)別:
使用 OAuth 對 EWS 應用程序進行身份驗證
一、使用OAuth2認證申請
1、申請AppID,tenantID
管理員在AAD(Azure Active Directory)進行App 注冊。注冊完畢之后會生產(chǎn)Application (client) ID(APPID)和一個Directory (tenant) ID (租戶ID)
?2、申請secrets
按照如下圖步驟,
注意:
? ? ? ? 1、在選擇過期日期時,默認為6個月,可手動修改為24個月
? ? ? ? 2、生產(chǎn)的Value值只會在首次添加完成之后出現(xiàn),所以添加完成之后,將步驟2中Value值保存下來
?
?3、添加授權權限
?點擊左側的API Permission 鏈接,添加如下的權限
? 注意:如果你是管理員,那么你可以添加Application類型的權限,否則你只能添加Delegation類型的權限
二、Policy授權
需要EWS的管理原將你要獲取的郵箱地址配置到EWS服務器中,將應用程序權限限制為特定 Exchange Online 郵箱
可參考:添加特定郵箱地址的ApplicationAccessPolicy
授權完畢之后,進行查看,如出現(xiàn)如下內(nèi)容,則表示添加完畢。
?三、C#代碼編寫
1、Config文件配置第一步驟中生成的三個ID
<!-- The application ID from your app registration -->
<add key="appId" value="{{YOUR APP ID}}" />
<!-- The tenant ID copied from your app registration -->
<add key="tenantId" value="{{YOUR TENANTID}}"/>
<!-- The application's client secret from your app registration. Needed for application permission access -->
<add key="clientSecret" value="{{YOUR SECRET ID}}"/>
2、代碼執(zhí)行邏輯
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Identity.Client;
using Quartz;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public async System.Threading.Tasks.Task GetEmailByOAuth()
{
var cca = ConfidentialClientApplicationBuilder
.Create(ConfigurationManager.AppSettings["appId"])
.WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
.WithTenantId(ConfigurationManager.AppSettings["tenantId"])
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();
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, "{{你將要訪問的郵箱地址,注意該地址必須是第二步驟中添加Policy中的地址,否則會提示403,禁止訪問}}");
ewsClient.HttpHeaders.Add("X-AnchorMailbox", "{{你將要訪問的郵箱地址,注意該地址必須是第二步驟中添加Policy中的地址,否則會提示403,禁止訪問}}");
int offset = 0;
int pageSize = 100;
bool more = true;
ItemView itemViews = new ItemView(pageSize, offset);
PropertySet itemPorpertySet = new PropertySet(BasePropertySet.FirstClassProperties,
EmailMessageSchema.MimeContent);
//獲取郵箱中的文件夾,如:收件箱,草稿箱,日歷等等
//var findFolders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, SetFilter(), view);
//獲取收件箱中的具體郵件內(nèi)容
FindItemsResults<Item> findResults = ewsClient.FindItems(WellKnownFolderName.Inbox, SetFilter(), itemViews);
}
3、測試
當如上代碼執(zhí)行到最下面一行,如果驗證findResults.Items.Count()>0,則驗證成功,表示可正常獲取郵箱地址中的郵件內(nèi)容。至此,郵件審批功能修改完畢。文章來源:http://www.zghlxwxcb.cn/news/detail-460807.html
四、總結
以上步驟都是一個坑一個坑趟出來的,只寫了正常執(zhí)行的情況,當然,在正常從頭開始做的時候,肯定會遇到很多的問題,都沒有單獨的列出來,如果讀者也在使用這種方式,途中也遇到了各種各樣的問題,請看官老爺留言,咱么一起學習,一起進步文章來源地址http://www.zghlxwxcb.cn/news/detail-460807.html
到了這里,關于使用OAuth2認證結合EWS實現(xiàn)實時讀取郵件功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!