.NET SDK调用示例

本文介绍了增强版活体检测方案的.NET SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI开发者门户在线调试工具调试API接口。该工具会自动生成相应API的SDK调用示例代码,详细信息,请参见:

前提条件

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

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

安装SDK

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

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

  • 通过.NET命令行安装。

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

    Install-Package AlibabaCloud.SDK.Cloudauth20200618 -Version 1.1.5
    Install-Package Aliyun.Credentials
说明

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

调用示例

关于参数的具体描述,请参见InitSmartVerifyDescribeSmartVerify

InitSmartVerify接口调用示例

using System;

using System.Collections.Generic;
using Tea;

namespace InitSmartVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20200618.Client CreateClient(string endpoint)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
               //通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyRequest request = new AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyRequest
            {
                // 请输入场景ID。
                SceneId = xxx,
                OuterOrderNo = "商户请求的唯一标识",
                // 证件类型,固定值。
                Mode = "ACCOUNT_SAFE",
                CertType = "IDENTITY_CARD",
                //CertName = "张三",
                //CertNo="xxx",
                // MetaInfo环境参数,需要通过客户端SDK获取。
                MetaInfo = "{\"apdidToken\":\"\",\"appName\":\"com.aliyun.identity.platform\",\"appVersion\":\"1.0\","
                + "\"bioMetaInfo\":\"5.1.0:11501568,4\",\"deviceModel\":\"JKM-AL00b\",\"deviceType\":\"android\","
                + "\"osVersion\":\"9\",\"zimVer\":\"1.0.0\"}",
                Mobile = "130xxxxxxxx",
                //Ip = "114.xxx.xxx.xxx",
                //UserId = "12345xxxx",
                //CallbackUrl = "https://www.aliyundoc.com",
                //CallbackToken = "xxxxxxx"
            };
            AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyResponse response = InitSmartVerifyAutoRoute(request);
            // 不支持服务自动路由。
            //AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyResponse response = InitSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.RequestId);
            Console.WriteLine(response.Code);
            Console.WriteLine(response.Message);
            Console.WriteLine(response.ResultObject.CertifyId);
        }


        public static AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyResponse InitSmartVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20200618.Models.InitSmartVerifyResponse response = InitSmartVerify(endpoint, request);
                    if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Code))
                    {
                        continue;
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
            }
            return lastResponse;
        }

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

DescribeSmartVerify接口调用示例

using System;

using System.Collections.Generic;
using Tea;


namespace DescribeSmartVerify
{
    public class Program
    {

        public static AlibabaCloud.SDK.Cloudauth20200618.Client CreateClient(string endpoint)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
              //通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。
               Credential=new Aliyun.Credentials.Client(null)
            };
  					config.Endpoint = endpoint;
            return new AlibabaCloud.SDK.Cloudauth20190307.Client(config);
        }

        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyRequest request = new AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyRequest
            {
                // 请输入场景ID。
                SceneId = xxx,
                CertifyId = "91707dc296d469ad38e4c5efa6a0xxxx",
            };
            AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyResponse response = DescribeSmartVerifyAutoRoute(request);
            // 不支持服务自动路由。
            // AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyResponse response = DescribeSmartVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
            Console.WriteLine(response.RequestId);
            Console.WriteLine(response.Code);
            Console.WriteLine(response.Message);
            Console.WriteLine(response.ResultObject.Passed);
            Console.WriteLine(response.ResultObject.SubCode);
            Console.WriteLine(response.ResultObject.MaterialInfo);
            Console.WriteLine(response.ResultObject.PassedScore);
        }


        public static AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyResponse DescribeSmartVerifyAutoRoute(AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyRequest request)
        {
            List<string> endpoints = new List<string>
            {
                "cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com"
            };
            AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyResponse lastResponse = null;

            foreach (var endpoint in endpoints)
            {
                try
                {
                    AlibabaCloud.SDK.Cloudauth20200618.Models.DescribeSmartVerifyResponse response = DescribeSmartVerify(endpoint, request);
                    if (AlibabaCloud.TeaUtil.Common.EqualString("500", response.Code))
                    {
                        continue;
                    }
                    return response;
                }
                catch (TeaException err)
                {
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    // 网络异常,切换到下个区域调用。
                    if (AlibabaCloud.TeaUtil.Common.EqualString("ServiceUnavailable", err.Code))
                    {
                        continue;
                    }
                }
            }
            return lastResponse;
        }

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