阅读1010 返回首页    go 魔兽


.NET教程__sdk_数据风控-阿里云

创建Access Key

1.登陆阿里云账号;2.打开“我的Access Key”页面,页面地址:https://i.aliyun.com/access_key/;3.创建Access Key。

安装SDK

  • 开发环境阿里云.Net SDK支持framework 4.0或以上版本。
  • 安装步骤1、在解压的文件中可以找到aliyun-net-sdk-cf.dll和aliyun-net-sdk-core.dll这两个文件;2、在你的Visual Studio项目上右键点击,添加引用->浏览(如果你用的是MonoDevelop,请自行按照相应导入dll的方法导入这个dll文件);3、选择上述的dll文件,点击“确定”按钮,经过以上步骤,你就可以在工程中使用阿里云.Net SDK了。

快速入门

初始化一个IClientProfile1、与阿里云SDK相关的类都在包com.aliyuncs.IAcsClient下;2、IClientProfile是与API交互的接口,SDK的操作都是通过IClientProfile完成的;3、IClientProfile可以复用,建议设置成应用程序全局唯一的;4、用户可以调用带有connectTimeout和readTimeout的构造方法来设置SDK调用接口的连接超时时间和读取超时时间,SDK默认的连接超时时间是3秒,读取超时时间是80秒。

  1. String regionid = "cn-hangzhou";
  2. String accessKeyId = "*** Provide your AccessKeyId ***";
  3. String accessKeySecret = "*** Provide your AccessKeySecret ***";
  4. // Create a new IClientProfile instance
  5. IClientProfile client = DefaultProfile.GetProfile(regionid, accessKeyId, accessKeySecret);

示例代码

  • 风险识别API

    1. public static void testActionSample()
    2. {
    3. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", aliyun_accesskey, aliyun_secret);//阿里云accesskey和secret
    4. IAcsClient client = new DefaultAcsClient(profile);
    5. QuerySimpleRequest querySimpleRequest = new QuerySimpleRequest();
    6. querySimpleRequest.AppKey = afs_appkey; //从前端获取
    7. long TimeStamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
    8. String enc = GetMd5Hash(afs_appkey+afs_accesskey+TokenId+TimeStamp);//前端获取的AppKey+数据风控服务颁发的accesskey+TokenId+TimeStamp
    9. querySimpleRequest.SerialNo = enc;
    10. querySimpleRequest.TokenId = "xxxxx"; //前端获取
    11. querySimpleRequest.TimeStamp = TimeStamp; //时间戳
    12. querySimpleRequest.SceneId = "register"; //前端获取
    13. querySimpleRequest.Trans = "{"IP":"192.168.0.1"}"; //透传参数
    14. try
    15. {
    16. QuerySimpleResponse httpResponse = client.GetAcsResponse(querySimpleRequest);
    17. Console.WriteLine("risklevel:" + httpResponse.CollinadataQueryResult.Risklevel);
    18. }
    19. catch (ServerException e)
    20. {
    21. Console.WriteLine(e.ToString());
    22. }
    23. catch (ClientException e)
    24. {
    25. Console.WriteLine(e.ToString());
    26. }
    27. Console.Read();
    28. }
  • 风险拦截API

    1. public static void testActionSample2()
    2. {
    3. IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", aliyun_accesskey, aliyun_secret);//阿里云accesskey和secret
    4. IAcsClient client = new DefaultAcsClient(profile);
    5. String token = "xxxxx"; //前端获取
    6. String sig = "xxxxx"; //前端获取
    7. AuthenticateRequest authenticateRequest = new AuthenticateRequest();
    8. authenticateRequest.Serno = GetMd5Hash(afs_appkey + afs_accesskey + token + sig);//数据风控服务颁发的appkey、accesskey,请不要用阿里云的accesskey
    9. authenticateRequest.AppKey = afs_appkey; //数据风控服务颁发的appkey
    10. authenticateRequest.RemoteIp = "192.168.100.112"; //来源IP
    11. authenticateRequest.SessionId = "xxxxx"; //前端获取
    12. authenticateRequest.Token = token;
    13. authenticateRequest.Sig = sig;
    14. try
    15. {
    16. AuthenticateResponse httpResponse = client.GetAcsResponse(authenticateRequest);
    17. Console.WriteLine("code:" + httpResponse.SigAuthenticateResult.Code);
    18. }
    19. catch (ServerException e)
    20. {
    21. Console.WriteLine(e.ToString());
    22. }
    23. catch (ClientException e)
    24. {
    25. Console.WriteLine(e.ToString());
    26. }
    27. Console.Read();
    28. }

最后更新:2016-11-23 17:16:05

  上一篇:go JAVA教程__sdk_数据风控-阿里云
  下一篇:go PHP教程__sdk_数据风控-阿里云