c# - 如何从我的 MVC 应用程序访问 OneDrive for Business

标签 c# asp.net-mvc azure search sharepoint

我的授权服务 SharePoint Search REST API ( https://msdn.microsoft.com/en-us/library/office/jj163876.aspx?f=255&MSPPError=-2147217396 ) 出现问题。

我们有一个 ASP.NET MVC 应用程序,它使用 AZURE AD 进行身份验证(配置为与我们的应用程序一起使用)。我还订阅了 Office 365,特别是,我使用 OneDrive for Business 来存储文档。我在 AZURE AD 中的应用程序配置为与 Office 365 SharePoint Online 和 Windows Azure Active Directory 配合使用。

在授权期间,用户在登录页面login.microsoftonline.com 上输入其详细信息。如果身份验证成功,AZURE AD 会将页面返回给用户并返回访问 token 。接下来,我们要使用 SharePoint 搜索 REST API 在存储在 oneDrive 上的文档中进行文本搜索。使用 GET 请求类型 https://<my_domain>.sharepoint.com/sites/test/_api/search/query?querytext='doc'返回错误“401 未经授权”。当您在请求正文中添加从 AZURE AD 接收的访问 token 时,它并不能解决问题,服务器会返回错误“403 Forbidden”。

示例请求

    stringtenantId = ClaimsPrincipal.Current.FindFirst(AppConfiguration.TenantIdClaimType).Value;
    AuthenticationContextauthContext = newAuthenticationContext(string.Format(CultureInfo.InvariantCulture, AppConfiguration.LoginUrl, tenantId));
    ClientCredentialcredential = newClientCredential(AppConfiguration.AppPrincipalId, AppConfiguration.AppKey);
    AuthenticationResultassertionCredential = authContext.AcquireToken(AppConfiguration.GraphUrl, credential);
    stringauthHeader = assertionCredential.CreateAuthorizationHeader();

    HttpClientclient = newHttpClient();
    HttpRequestMessagerequest = newHttpRequestMessage(HttpMethod.Get, "https://<my_domain>.sharepoint.com/sites/test/_api/search/query?querytext='doc'");
    request.Headers.TryAddWithoutValidation("Authorization", authHeader);
    request.Headers.TryAddWithoutValidation("Accept", "application/json;odata=minimalmetadata");
    HttpResponseMessageresponse = awaitclient.SendAsync(request);

还有

public static class AppConfiguration
{
    public static string TenantIdClaimType
    {
        get
        {
            return "http://schemas.microsoft.com/identity/claims/tenantid";
        }
    }

    public static string LoginUrl
    {
        get
        {
            return "https://login.windows.net/{0}";
        }
    }

    public static string GraphUrl
    {
        get
        {
            return "https://graph.windows.net";
        }
    }

    public static string GraphUserUrl
    {
        get
        {
            return "https://graph.windows.net/{0}/users/{1}?api-version=2013-04-05";
        }
    }

    public static string AppPrincipalId
    {
        get
        {
            return ConfigurationManager.AppSettings["ida:ClientID"];
        }
    }

    public static string AppKey
    {
        get
        {
            return ConfigurationManager.AppSettings["ida:Password"];
        }
    }
}

据我了解,问题出在访问 token 上。但我没有找到任何有关如何正确配置此 token 的信息。 JavaScript 请求不适合我,该请求应该从服务器处理。

最佳答案

我认为有两件事你可以检查。

您需要 Azure AD 中的“应用程序权限”而不是“委派权限”。这是您的应用程序的配置方式吗?

根据经验,如果您在使用 AccessToken 访问 Office 365 中的某项功能时遇到困难,有时是因为您需要更安全的 token 。查看需要“ClientAssertionCertificate”类的 AcquireToken 的重载。您必须在 Azure AD list 中手动配置它。 查看此引用(有关“为您的应用程序配置 X.509 公共(public)证书”的部分):https://msdn.microsoft.com/en-us/office/office365/howto/building-service-apps-in-office-365

这有帮助吗?

关于c# - 如何从我的 MVC 应用程序访问 OneDrive for Business,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32649871/

相关文章:

c# - 使用 WCF 的原始 MQ 消息

c# - 如何在具有 2 个参数的函数上使用 System.Threading.Tasks.Task 进行异步调用?在.Net

asp.net-mvc - ASP.NET MVC中的自定义错误处理

javascript - 来自 jQuery 的编程表单 POST 在文档就绪中不起作用

javascript - 如何为 Ajax.ActionLink 应用 CSS

IIS 共享配置覆盖 Azure 中 FTP 服务器的 IP 地址

azure - 如何使用 Visual Studio 2015 在 Azure 函数中定义连接字符串?

c# - 是否可以向上抛出异常

c# - 如何从存储过程中返回十进制值

azure - 文档数据库 : Removing default indexing