通过 Web 服务对用户进行验证

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

您可以使用 Microsoft Dynamics 365 的外部客户端身份验证功能为移动设备(例如平板电脑、电话以及 Windows 8 桌面)开发您自己的客户端应用程序。 此功能还可用于非 .NET 应用程序。

本主题内容

身份验证概述

技术依赖项

安全性

用户登录和应用程序注册

客户端应用程序

OAuth 授权终结点

发现 OAuth 终结点 URL

指定 OAuth 资源

身份验证概述

创建现代和移动的应用程序(包括未在 .NET Framework 上构建的应用程序)的开发人员,可以通过组织 Web 服务的 SOAP 和 OData 终结点访问 Microsoft Dynamics 365 业务数据。 此 Web 服务支持 OAuth 2.0 协议 中的某些身份验证功能。

以下列表描述了支持现代和移动应用程序身份验证:

  • 在 HTTP 授权头中使用 JSON web 令牌。

  • 通过外部应用程序(浏览器之外)对 OData 服务进行身份验证

  • 通过外部应用程序(浏览器之外)对 Organization.svc/web (SOAP) 服务进行身份验证

技术依赖项

要求使用下列技术开发和执行使用 Microsoft Dynamics 365OData 和 SOAP Web 服务终结点进行身份验证的外部客户端应用程序:

可以选择使用下列技术开发和执行使用 Microsoft Dynamics 365OData 和 SOAP 服务终结点进行身份验证的外部客户端应用程序:

安全性

以下安全信息适用于此身份验证功能:

  • 身份验证令牌存储在保护性存储的设备上。 Windows 操作系统使用 Windows 凭据管理器。

  • 此功能使用 传输层安全 (TLS) 或安全套接字层 (SSL) 发出 HTTP 请求。

用户登录和应用程序注册

以下信息与用户的登录和应用程序注册相关。

  • Web 浏览器处理用户登录的 Microsoft Azure Active Directory 身份验证库 (ADAL)。

  • 通过 Dynamics 365(在线) 部署的 Azure Active Directory 和内部部署的 Active Directory 联合身份验证服务 (AD FS) 或 面向 Internet 的部署 (IFD) 管理应用程序注册。 您可以使用 Microsoft Azure 管理门户 或 API 注册您的 Dynamics 365(在线) 应用程序。

客户端应用程序

以下列表总结了外部客户端应用程序能够执行的一系列操作:

  • 当使用 OData 终结点时,支持创建、检索、更新和删除操作。 不支持消息执行或元数据检索。

  • 在使用现代和移动应用程序的 SOAP 终结点 (Organization.svc/web) 时,可以访问完整的 Web 服务功能集。

在编写调用 Web 服务的客户端代码时,建议在每个服务调用前通过 ADAL 请求令牌。 这样,ADAL 就能够确定是否重新使用访问令牌的缓存实例,是否为使用刷新令牌的新令牌提出请求,是否提示用户进行登录。

发现 OAuth 终结点 URL

在应用程序或配置文件,提供在运行时发现 Web 服务身份验证的功能,并将其作为相对于 OAuth 提供程序 URLs 的获取机构一种备选方法。

通过在 Authorization 页眉和租户组织的 SOAP 终结点 URL 发送未授权的 附有“Bearer”字的HTTP 请求作为请求消息来启动发现过程。

GET /XRMServices/2011/Organization.svc HTTP/1.1 Host: <org>.crm.dynamics.com Authorization: Bearer

备注

持有者面临的难题是可选的。 执行没有授权头的 GET 将产生相同的结果。

401 错误与包含 authorization_uri 参数的响应一起被返回。 该参数值是机构 URL。

HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer authorization_uri=URI

只有在租户组织的 SOAP 终结点 URL 中显示 SdkClientVersion 属性时,SOAP 客户端才可以使用机构发现功能。 示例 URL 所示机构。

https://contoso.crm.dynamics.com/XRMServices/2011/Organization.svc/web?SdkClientVersion=6.1.0.533;

只有当至少有一个小数点和比 6.0.0002.0000 大时,SdkClientVersion 值才可以是任何版本号。 建议 SdkClientVersion 属性值设置为链接到客户端应用程序 SDK 程序集的产品构建。

下面代码示例演示如何获得机构 URL。 请注意示例代码利用可以从NuGet.org 获取的 Microsoft Azure Active Directory 身份验证库 (ADAL)。 还有 AndroidiOS 此库的开启源版本。


/// <summary>
/// Discover the authentication authority.
/// </summary>
/// <param name="serviceUrl">The URL of the organization's SOAP endpoint. </param>
/// <returns>The authority URL.</returns>
/// <remarks>The service URL must contain the SdkClient property.</remarks>
/// <example>https://contoso.crm.dynamics.com/XRMServices/2011/Organization.svc/web?SdkClientVersion=6.1.0.533;</example>
public static string DiscoveryAuthority(Uri serviceUrl)
{
    // Use AuthenticationParameters to send a request to the organization's endpoint and
    // receive tenant information in the 401 challenge. 
    Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters parameters = null;
    HttpWebResponse response = null;
    try
    {
        // Create a web request where the authorization header contains the word "Bearer".
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);

        // The response is to be encoded.
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        response = (HttpWebResponse)httpWebRequest.GetResponse();
    }

    catch (WebException ex)
    {
        response = (HttpWebResponse)ex.Response;

        // A 401 error should be returned. Extract any parameters from the response.
        // The response should contain an authorization_uri parameter.
        parameters = Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters.
            CreateFromResponseAuthenticateHeader((response.Headers)["WWW-Authenticate"]);
    }
    finally
    {
        if (response != null)
            response.Dispose();
    }
    // Return the authority URL.
    return parameters.Authority;
}

OAuth 授权终结点

为使用 OAuth 发现的一种备选方法将用于众所周知的 OAuth 授权终结点。 如下表所示,在使用 Microsoft Dynamics 365(在线或本地) Web 服务编写进行验证的应用程序时,您需要在身份验证代码中使用 OAuth 提供商 URLs。

部署

URL

Microsoft Dynamics 365 (online)

HYPERLINK "https://login.windows.net/common/oauth2/authorize" https://login.windows.net/common/oauth2/authorize(多租户)

https://login.windows.net/<tenant ID>/oauth2/authorize(单个租户)

Microsoft Dynamics 365(内部部署/IFD)

https://<serverFQDNaddress>/adfs/ls

替换 IFD 服务器地址,例如在 安全令牌服务 (STS)URL 中使用 contoso.com。 STS URL 是 AD FS 的默认安装。 非默认安装中可以使用不同的 URL。 同样,请替代指示您 ID 的租户。

则建议您始终使用将 OAuth 发现与 Dynamics 365(在线) 联用,因为授权终结点可以在未来某个时间更改。

指定 OAuth 资源

在使用 OAuth 授权代码流对 Microsoft AzureActive Directory 进行验证时,您需为目标资源提供值。 在调用 OAuth 授权终结点时,根组织 Web 地址(如https://contoso.crm.dynamics.com)需被用作“资源”查询字符串参数。

// Obtain an authentication token to access the web service.
String resource = “https://contoso.crm.dynamics.com”; 
_authenticationContext = new AuthenticationContext(_oauthUrl, false );
AuthenticationResult result = await _authenticationContext.AcquireTokenAsync( resource, clientID );

详细信息:示例:Windows 8 桌面现代 OData 应用程序

另请参阅

编写移动和现代应用程序
演练:使用 Active Directory 注册 Dynamics 365 应用程序
在 Microsoft Dynamics 365 中对用户进行身份验证
博客:介绍 Azure AD 开发人员预览版中的新功能:Azure 身份验证库
使用 Azure Active Directory 的服务器端 CRM 身份验证

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权