文档

.NET SDK调用示例

更新时间:

本文介绍了金融级实人认证纯服务端API的.NET SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口,该工具会自动生成相应API的SDK调用示例代码。您可以单击以下接口链接进行调试:

调试ContrastFaceVerify接口

前提条件

  • 需要使用的.NET Framework为4.5.0及以上版本或者.NET Standard为2.0及以上版本。

  • 已添加认证场景,并获取对应的场景ID。具体操作,请参见添加认证场景

安装SDK

关于.NET SDK源代码,请参见.NET SDK包

SDK有以下两种安装方式,请选择合适的方式安装:

  • 通过.NET命令行安装。

    dotnet add package AlibabaCloud.SDK.Cloudauth20190307 --version 2.0.3
    dotnet add package Aliyun.Credentials
  • 使用包管理安装。

    Install-Package AlibabaCloud.SDK.Cloudauth20190307 -Version 2.0.3
    Install-Package Aliyun.Credentials
说明

Credentials工具配置方法,请参见身份验证配置

照片实人认证调用示例

关于参数的具体描述,请参见照片实人认证

using System;

using System.Collections.Generic;
using Tea;


namespace ContrastFaceVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20190307.Client CreateClient(string endpoint)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request = new AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest
            {
                // 固定值。
                ProductCode = "ID_MIN",
                // 请输入场景ID。
                SceneId = xxx,
                OuterOrderNo = "商户请求的唯一标识",
                Model = "NO_LIVENESS",
                // 固定值IDENTITY_CARD。
                CertType = "IDENTITY_CARD",
                CertName = "张三",
                CertNo = "xxx",
                //Ip = "用户IP地址",
                //UserId = "用户ID",
                //Mobile = "130xxxxxxxx",
                //DeviceToken = "通过设备指纹获取的deviceToken",

                // 请在以下四种方式中选择其一。
                // 1.照片Base64编码。
                // 2.照片OSS Bucket名和文件名。
                // 3.照片OSS的URL地址。
                // 4.已经实人认证通过的CertifyId。

                // 方式一:照片Base64。
                //FaceContrastPicture = "人脸照片Base64编码",
                // 方式二:照片OSS Bucket名和文件名。
                // OssBucketName = "cn-shanghai-aliyun-cloudauth-xxxxx",
                // OssObjectName = "verify/xxxxx/xxxxxx.jpeg",
                // 方式三:照片OSS的URL地址,公网可访问。
                FaceContrastPictureUrl = "https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg",
                // 方式四:已经实人认证通过的CertifyId。
                // CertifyId = "0bfa7c493f850e5178b9f8613634c9xx",

                // 如需开启个人信息加密传输。
                // EncryptType = "SM2",
                // CertName = "BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==",
                // CertNo = "BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=="
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerifyAutoRoute(request);
            // 不支持服务自动路由。
            //AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.Body.RequestId);
            Console.WriteLine(response.Body.Code);
            Console.WriteLine(response.Body.Message);
            Console.WriteLine(response.Body.ResultObject.Passed);
            Console.WriteLine(response.Body.ResultObject.IdentityInfo);
            Console.WriteLine(response.Body.ResultObject.SubCode);
            Console.WriteLine(response.Body.ResultObject.MaterialInfo);
            Console.WriteLine(response.Body.ResultObject.CertifyId);
        }


        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify(endpoint, request);
                    lastResponse = response;
                    if (response is not null)
                    {
                        if (AlibabaCloud.TeaUtil.Common.EqualNumber(500, (int)(response.StatusCode)))
                        {
                            continue;
                        }

                        if (response.Body is not null)
                        {
                            if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Body.Code))
                            {
                                continue;
                            }
                        }
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    Console.WriteLine(err);
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    Console.WriteLine(_err);
                    continue;
                }
            }
            return lastResponse;
        }

        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerify(string endpoint, AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Client client = CreateClient(endpoint);
            // 创建RuntimeObject实例并设置运行参数。
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            runtime.ReadTimeout = 10000;
            runtime.ConnectTimeout = 10000;
            return client.ContrastFaceVerifyWithOptions(request, runtime);
        }
    }
}

视频实人认证调用示例

支持OSS对象存储方式和本地文件两种方式。关于参数的具体描述,请参见视频实人认证

使用本地文件的方式(推荐)

using System;

using System.Collections.Generic;
using Tea;
using System.IO;

namespace ContrastFaceVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20190307.Client CreateClient(string endpoint)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            FileStream fs = new FileStream("本地文件路径", FileMode.Open);
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyAdvanceRequest request = new AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyAdvanceRequest
            {
                // 固定值。
                ProductCode = "ID_MIN_VIDEO",
                // 请输入场景ID。
                SceneId = xxx,
                OuterOrderNo = "商户请求的唯一标识",
                Model = "FRONT_CAMERA_LIVENESS",
                // 固定值IDENTITY_CARD。
                CertType = "IDENTITY_CARD",
                CertName = "张三",
                CertNo = "xxx",
                //Ip = "用户IP地址",
                //UserId = "用户ID",
                //Mobile = "130xxxxxxxx",
                //DeviceToken = "通过设备指纹获取的deviceToken",
                FaceContrastFileObject = fs,
                // 如需开启个人信息加密传输。
                // EncryptType = "SM2",
                // CertName = "BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==",
                // CertNo = "BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=="
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerifyAutoRoute(request);
            // 不支持服务自动路由。
            //AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.Body.RequestId);
            Console.WriteLine(response.Body.Code);
            Console.WriteLine(response.Body.Message);
            Console.WriteLine(response.Body.ResultObject.Passed);
            Console.WriteLine(response.Body.ResultObject.IdentityInfo);
            Console.WriteLine(response.Body.ResultObject.SubCode);
            Console.WriteLine(response.Body.ResultObject.MaterialInfo);
            Console.WriteLine(response.Body.ResultObject.CertifyId);

            fs.Close();
        }


        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyAdvanceRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify(endpoint, request);
                    lastResponse = response;
                    if (response is not null)
                    {
                        if (AlibabaCloud.TeaUtil.Common.EqualNumber(500, (int)(response.StatusCode)))
                        {
                            continue;
                        }

                        if (response.Body is not null)
                        {
                            if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Body.Code))
                            {
                                continue;
                            }
                        }
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    Console.WriteLine(err);
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    Console.WriteLine(_err);
                    continue;
                }
            }
            return lastResponse;
        }

        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerify(string endpoint, AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyAdvanceRequest request)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Client client = CreateClient(endpoint);
            // 创建RuntimeObject实例并设置运行参数。
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            runtime.ReadTimeout = 10000;
            runtime.ConnectTimeout = 10000;
            return client.ContrastFaceVerifyAdvance(request, runtime);
        }
    }
}

使用OSS对象存储的方式

using System;

using System.Collections.Generic;
using Tea;


namespace ContrastFaceVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20190307.Client CreateClient(string endpoint)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request = new AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest
            {
                // 固定值。
                ProductCode = "ID_MIN_VIDEO",
                // 请输入场景ID。
                SceneId = xxx,
                OuterOrderNo = "商户请求的唯一标识",
                Model = "FRONT_CAMERA_LIVENESS",
                // 固定值IDENTITY_CARD。
                CertType = "IDENTITY_CARD",
                CertName = "张三",
                CertNo = "xxx",
                //Ip = "用户IP地址",
                //UserId = "用户ID",
                //Mobile = "130xxxxxxxx",
                //DeviceToken = "通过设备指纹获取的deviceToken",
                OssBucketName = "cn-shanghai-aliyun-cloudauth-xxxxx",
                OssObjectName = "verify/xxxxx/xxxxxx.mp4",
                // 如需开启个人信息加密传输。
                // EncryptType = "SM2",
                // CertName = "BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==",
                // CertNo = "BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg=="
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerifyAutoRoute(request);
            // 不支持服务自动路由。
            //AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.Body.RequestId);
            Console.WriteLine(response.Body.Code);
            Console.WriteLine(response.Body.Message);
            Console.WriteLine(response.Body.ResultObject.Passed);
            Console.WriteLine(response.Body.ResultObject.IdentityInfo);
            Console.WriteLine(response.Body.ResultObject.SubCode);
            Console.WriteLine(response.Body.ResultObject.MaterialInfo);
            Console.WriteLine(response.Body.ResultObject.CertifyId);
        }


        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse response = ContrastFaceVerify(endpoint, request);
                    lastResponse = response;
                    if (response is not null)
                    {
                        if (AlibabaCloud.TeaUtil.Common.EqualNumber(500, (int)(response.StatusCode)))
                        {
                            continue;
                        }

                        if (response.Body is not null)
                        {
                            if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Body.Code))
                            {
                                continue;
                            }
                        }
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    Console.WriteLine(err);
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    Console.WriteLine(_err);
                    continue;
                }
            }
            return lastResponse;
        }

        public static AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyResponse ContrastFaceVerify(string endpoint, AlibabaCloud.SDK.Cloudauth20190307.Models.ContrastFaceVerifyRequest request)
        {
            AlibabaCloud.SDK.Cloudauth20190307.Client client = CreateClient(endpoint);
            // 创建RuntimeObject实例并设置运行参数。
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            runtime.ReadTimeout = 10000;
            runtime.ConnectTimeout = 10000;
            return client.ContrastFaceVerifyWithOptions(request, runtime);
        }
    }
}