簡體   English   中英

使用外接程序中的Outlook Online令牌對用戶進行身份驗證以在線進行Exchange

[英]Authenticate user to Exchange online with Outlook online token from an add-in

更新資料

似乎UserIdentityToken無法用於標識要在線交換的用戶。 第三方服務(即自定義聊天帳戶)可以使用該令牌,以一種可靠的方式來標識當前用戶。

如果要從Exchange Online檢索內容,則可以依賴回調令牌 ,但是有5分鍾的時間限制

如果我們絕對想避免此限制,那么我發現的唯一其他方法是詢問他們的Office 365憑據。 這是我的方法:

JS

// Send Basket Button | Send the basket to Sharepoint
$("#sendMails").click(function () {
    var mailsId = getMailIds();
    if (mailsId != null) saveMails(mailsId);
});

function saveMails(mailsId) {
    $.post(
        "/AJAX/SaveMails"
        {
            mailsId: JSON.stringify(mailsId),
            login: "mymail@onmicrosoft.com",
            password: "mypass",
        },
        function(result) {
            console.log("saveMails : ", result);
        },
        "text"
    );
}

C#ASP.NET MVC

[HttpPost]
public ActionResult SaveMails()
{
    // Office365 credentials
    var login = Request["login"];
    var password = Request["password"];

    // mailsID to retrieve from Exchange
    // IDs come from Office.context.mailbox.item.itemId
    var mailsID = JsonConvert.DeserializeObject<List<string>>(Request["mailsID"]);

    // Set credentials and EWS url for the Exchange connection
    var exService = new ExchangeService
    {
        // User's credentials
        Credentials = new WebCredentials(login, password),

        // Exchange Uri (always the same for Office365)
        Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx")
    }

    foreach(var mail in mailsID)
    {
         var itemId = new ItemId(mail);
         // Get mails from Exchange Online
         var email = EmailMessage.Bind(exService, itemId);

         // ... Do something with the mail
    }

    // ... Rest of the code
}

現在,您可以處理您的郵件。


(舊帖子)

我為此感到掙扎。 我希望使用ASP.NET MVC Web Server從Outlook Online檢索一堆電子郵件,而沒有項目令牌的限制,該令牌的生命周期只有5分鍾。

我目前正在嘗試的是:

顯然,它不起作用。 但是,我嘗試通過輸入登錄名和我的Office帳戶的密碼進行身份驗證,在這里可以正常工作。

我在其他地方讀過,在嘗試對其進行身份驗證之前,我們必須先驗證我們的令牌,但它似乎僅涉及具有Azure AD的外部應用程序? 就我而言,這只是一個Outlook Online WEB加載項

好吧,這是我當前在控制器中的代碼(用於處理身份驗證並嘗試從Exchange檢索郵件)

[HttpPost]
public ActionResult GetMails()
{
    // Token from getUserIdentityTokenAsync() as a string
    var token = Request["token"];

    // mailsID to retrieve from Exchange
    // IDs come from Office.context.mailbox.item.itemId
    var mailsID = JsonConvert.DeserializeObject<List<string>>(Request["mailsID"]);


    var exService = new ExchangeService
    {
        Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
        Credentials = new OAuthCredentials(token),

        // WebCredentials works but I don't want the user to enter that
        // Credentials = new WebCredentials("mymail@onmicrosoft.com", "mypass");
    }

    foreach(var mail in mailsID)
    {
         var itemId = new ItemId(mail);
         // Try to get the mail from Exchange Online
         var email = EmailMessage.Bind(exService, itemId);

         // ... Rest of the code
    }

    // ... Rest of the method
}

Office.context.mailbox.item.itemId參考

我的目標是避免用戶再次輸入其Office Online憑據,這很奇怪,而且...不安全,我認為。 我想念的是什么?

提前致謝。

從Office.context.mailbox.getUserIdentityTokenAsync()獲得的ID令牌只能用於識別和驗證第三方系統的加載項和用戶

如果要使用令牌通過EWS從Exchange獲取項目,則需要使用getCallbackTokenAsync方法中的令牌。 這是示例供您參考:

function getMails2() {
    Office.context.mailbox.getCallbackTokenAsync(function (rs) {
        callMyWebService(rs.value, Office.context.mailbox.item.itemId)
    })
}

function callMyWebService(token, itemID) {
    var customer = { "token": token, "id": itemID };
    $.ajax({
        url: '../../api/Default',
        type: 'POST',
        data: JSON.stringify(customer),
        contentType: 'application/json;charset=utf-8'
    }).done(function (response) {
        if (!response.isError) {
            var names = "<h2>Attachments processed using " +
                          serviceRequest.service +
                          ": " +
                          response.attachmentsProcessed +
                          "</h2>";
            for (i = 0; i < response.attachmentNames.length; i++) {
                names += response.attachmentNames[i] + "<br />";
            }
            document.getElementById("names").innerHTML = names;
        } else {
            app.showNotification("Runtime error", response.message);
        }
    }).fail(function (status) {

    }).always(function () {
        $('.disable-while-sending').prop('disabled', false);
    })
};

Web API:

// POST api/<controller>
    public void Post([FromBody]Customer customer)
    {
        var token = customer.Token;

        // mailsID to retrieve from Exchange
        // IDs come from Office.context.mailbox.item.itemId
        // var mailsID = JsonConvert.DeserializeObject<List<string>>(customer.Id);



        var exService = new ExchangeService
        {
            Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
            Credentials = new OAuthCredentials(token),

            // WebCredentials works but I don't want the user to enter that
            // Credentials = new WebCredentials("mymail@onmicrosoft.com", "mypass");
        };

        var itemId = new ItemId(customer.Id);
        // Try to get the mail from Exchange Online
        var email = EmailMessage.Bind(exService, itemId);
        string subject = email.Subject; 
        // ... Rest of the code

    }

public class Customer
{
    public string Token { get; set; }
    public string Id { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM