在代表模型的源文件上傳到BIMFACE后,一般會進行三種API調用操作:
-
發起模型轉換
-
查詢轉換狀態
-
如轉換成功,獲取模型轉換后的BIM數據
- 在模型成功進行轉換后,模型內的BIM信息會在雲端進行解析,抽取並結構化入庫。這些信息包含:
-
-
構件屬性信息
-
構件分類樹
-
樓層
-
單體
-
專業
-
構件材質
-
模型鏈接
-
空間
-
房間
-
圖紙
-
…
-
在確認模型轉換成功后,為了開發者能方便的獲取這些BIM信息並集成在自己的應用中,BIMFACE提供了一系列的數據接口,這些接口支持兩種驗權方式:
- Access token: 代表自身應用的身份,使用應用的appkey, secret,通過調用/oauth2/token接口獲取。
- View token: 代表對單個模型的訪問權限,使用access token,通過調用/view/token以及相關接口獲得。
發起轉換
請求地址:PUT https://api.bimface.com/translate
說明:源文件上傳成功后,即可發起對該文件的轉換。由於轉換不能立即完成,BIMFace支持在文件轉換完成以后,通過Callback機制通知應用;另外,應用也可以通過接口查詢轉換狀態。
參數:application/json 格式
請求 path(示例):https://api.bimface.com/translate
請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"
請求 body(示例):
{ "callback" : "https://api.glodon.com/viewing/callback?authCode=iklJk0affae&signature=2ef131395fb6442eb99abd83d45c3201", "config" : { "string" : "string" }, "priority" : 2, "source" : { "compressed" : false, "fileId" : 1277823232112, "rootName" : "rootFileName.rvt" } }
注意:請求體中的 config 可以設置為空。"config":null 或者傳入指定的轉換參數 "config":{"texture":true} 。
HTTP響應示例(200):
{ "code" : "success", "data" : { "createTime" : "2017-12-25 17:23:46", "databagId" : "9b711803a43b92d871cde346b63e5019", "fileId" : 1248789071339712, "name" : "bimface_2018.rvt", "priority" : 2, "reason" : "reason", "status" : "success", "thumbnail" : [ "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/96.png", "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/256.png" ] }, "message" : "" }

DGW與RVT格式的文件轉換的配置參數不同,所以封裝了2個對應的C#類:
1 /// <summary> 2 /// 發起DWG文件轉化的請求數據 3 /// </summary> 4 [Serializable] 5 public class DwgFileTranslateRequest : FileTranslateRequest 6 { 7 /// <summary> 8 /// Dwg模型轉換引擎自定義參數,config參數跟轉換引擎相關,不同的轉換引擎支持不同的config格式。 9 /// 例如轉換時添加內置材質,則添加參數值{"texture":true},添加外部材質時參考“使用模型外置材質場景”請求報文。 10 /// 如果不需要設置該參數,則設置為null 11 /// </summary> 12 [JsonProperty("config", NullValueHandling = NullValueHandling.Ignore)] 13 public DwgModelConfig Config { get; set; } 14 }
1 /// <summary> 2 /// 發起Rvt文件轉化的請求數據 3 /// </summary> 4 [Serializable] 5 public class RvtFileTranslateRequest : FileTranslateRequest 6 { 7 /// <summary> 8 /// Rvt模型轉換引擎自定義參數,config參數跟轉換引擎相關,不同的轉換引擎支持不同的config格式。 9 /// 例如轉換時添加內置材質,則添加參數值{"texture":true},添加外部材質時參考“使用模型外置材質場景”請求報文。 10 /// 如果不需要設置該參數,則設置為null 11 /// </summary> 12 [JsonProperty("config", NullValueHandling = NullValueHandling.Ignore)] 13 public RvtModelConfig Config { get; set; } 14 }
1 /// <summary> 2 /// 其他三維模型文件,包括RVT格式文轉化的請求數據 3 /// </summary> 4 [Serializable] 5 public class Other3DModelFileTranslateRequest : RvtFileTranslateRequest 6 { 7 }
1 [Serializable] 2 public class TranslateSource 3 { 4 public TranslateSource() 5 { 6 Compressed = false; 7 RootName = null; 8 } 9 10 /// <summary> 11 /// 文件Id,即調用上傳文件API返回的fileId 12 /// </summary> 13 [JsonProperty("fileId")] 14 public long FileId { get; set; } 15 16 /// <summary> 17 /// 是否為壓縮文件,默認為false 18 /// </summary> 19 [JsonProperty("compressed")] 20 public bool Compressed { get; set; } 21 22 /// <summary> 23 /// 如果是壓縮文件,必須指定壓縮包中哪一個是主文件。(例如:root.rvt)。 24 /// 如果不是壓縮,則設置為 null 25 /// </summary> 26 [JsonProperty("rootName", NullValueHandling = NullValueHandling.Ignore)] 27 public string RootName { get; set; } 28 }
共同的基類FileTranslateRequest:
1 /// <summary> 2 /// 發起文件轉化的請求數據 3 /// </summary> 4 [Serializable] 5 public class FileTranslateRequest 6 { 7 public FileTranslateRequest() 8 { 9 Priority = 2; 10 CallBack = "http://www.app.com/receive"; 11 } 12 13 [JsonProperty("source")] 14 public TranslateSource Source { get; set; } 15 16 /// <summary> 17 /// 優先級,數字越大,優先級越低。只能是1, 2, 3。默認為2 18 /// </summary> 19 [JsonProperty("priority")] 20 public int Priority { get; set; } 21 22 /// <summary> 23 /// Callback地址,待轉換完畢以后,BIMFace會回調該地址 24 /// </summary> 25 [JsonProperty("callback")] 26 public string CallBack { get; set; } 27 }
不同模型轉換支持的自定義參數config:
(1) rvt模型
對應封裝成的C#實體類:
1 /// <summary> 2 /// rvt 模型配置項 3 /// </summary> 4 [Serializable] 5 public class RvtModelConfig 6 { 7 public RvtModelConfig() 8 { 9 //設置 null,在序列化的時候忽略該字段,不出現在序列化后的字符串中 10 Texture = null; 11 ExportDwg = null; 12 ExportDrawing = null; 13 ExportPdf = null; 14 ViewName = null; 15 DisplayLevel = null; 16 ExportDwgInstance = null; 17 ExportHiddenObjects = null; 18 ExportSystemType = null; 19 ExportProperties = null; 20 Unit = null; 21 ExportSchedule = null; 22 } 23 24 /// <summary> 25 /// 轉換時是否添加材質。默認為 false 26 /// </summary> 27 [JsonProperty("texture", NullValueHandling = NullValueHandling.Ignore)] 28 public bool? Texture { get; set; } 29 30 /// <summary> 31 /// rvt2md是否導出dwg文件。默認為 false 32 /// </summary> 33 [JsonProperty("exportDwg", NullValueHandling = NullValueHandling.Ignore)] 34 public bool? ExportDwg { get; set; } 35 36 /// <summary> 37 /// dwg2md是否導出mdv(矢量化圖紙);取值為true時,exportDwg自動設置為true。默認為 false 38 /// </summary> 39 [JsonProperty("exportDrawing", NullValueHandling = NullValueHandling.Ignore)] 40 public bool? ExportDrawing { get; set; } 41 42 /// <summary> 43 /// dwg2md是否導出pdf文件。默認為 false 44 /// </summary> 45 [JsonProperty("exportPdf", NullValueHandling = NullValueHandling.Ignore)] 46 public bool? ExportPdf { get; set; } 47 48 /// <summary> 49 /// 轉換使用的3D視圖。默認為 {3D} 50 /// </summary> 51 [JsonProperty("viewName", NullValueHandling = NullValueHandling.Ignore)] 52 public string ViewName { get; set; } 53 54 /// <summary> 55 /// 設置轉換的精細度,fine(精細),medium(中等),coarse(粗略)。默認為 fine 56 /// </summary> 57 [JsonProperty("displaylevel", NullValueHandling = NullValueHandling.Ignore)] 58 public string DisplayLevel { get; set; } 59 60 /// <summary> 61 /// 是否導出dwg實例。默認為 false 62 /// </summary> 63 [JsonProperty("exportDwgInstance", NullValueHandling = NullValueHandling.Ignore)] 64 public bool? ExportDwgInstance { get; set; } 65 66 /// <summary> 67 /// 是否導出三維視圖中隱藏的構件。默認為 false 68 /// </summary> 69 [JsonProperty("exportHiddenObjects", NullValueHandling = NullValueHandling.Ignore)] 70 public bool? ExportHiddenObjects { get; set; } 71 72 /// <summary> 73 /// 是否在userData中加入mepSystemType。默認為 false 74 /// </summary> 75 [JsonProperty("exportSystemType", NullValueHandling = NullValueHandling.Ignore)] 76 public bool? ExportSystemType { get; set; } 77 78 /// <summary> 79 /// 是否在導出NWD的屬性db文件。默認為 false 80 /// </summary> 81 [JsonProperty("exportProperties", NullValueHandling = NullValueHandling.Ignore)] 82 public bool? ExportProperties { get; set; } 83 84 /// <summary> 85 /// 設置轉換使用的單位,取值"ft"\"feet"\"英尺"采用revit默認的英尺為單位,默認以毫米為單位。默認為空 86 /// </summary> 87 [JsonProperty("unit", NullValueHandling = NullValueHandling.Ignore)] 88 public string Unit { get; set; } 89 90 /// <summary> 91 /// 是否使用明細表內容。默認為 false 92 /// </summary> 93 [JsonProperty("exportSchedule", NullValueHandling = NullValueHandling.Ignore)] 94 public bool? ExportSchedule { get; set; } 95 }
Rvt轉換配置中很多選項都是有默認值的,如果手動設置的值與默認值相同,那么可以不用設置該項。
為了簡化顯示請求body中的config配置項,在構造函數中將數值類型的配置項默認設置為 null,再配合 Newtonsoft.Json.dll
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 屬性,在序列化時可以不生成該項。
(2) dwg模型
對應封裝成的C#實體類
1 /// <summary> 2 /// dwg 模型配置項 3 /// </summary> 4 [Serializable] 5 public class DwgModelConfig 6 { 7 /// <summary> 8 /// 是否轉成矢量圖紙,默認為 true 9 /// </summary> 10 [JsonProperty("exportDrawing")] 11 public bool ExportDrawing { get; set; } 12 13 /// <summary> 14 /// 是否導出pdf文件,默認為 false 15 /// </summary> 16 [JsonProperty("exportPdf")] 17 public bool ExportPdf { get; set; } 18 19 /// <summary> 20 /// 是否導出縮略圖,默認為 true 21 /// </summary> 22 [JsonProperty("exportThumb")] 23 public bool ExportThumb { get; set; } 24 }
下面分別介紹常用的不同類型的文件轉換場景
請求 body(示例):
{ "source":{ "fileId":1402934652281952, // 文件ID "compressed":false // 文件是否為壓縮文件 }, "priority":2, // 轉換優先級 "callback":"http://www.app.com/receive", // 回調地址 "config":null // 轉換配置選項 }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。將DWG文件轉換成圖片。 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">DWG文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateDwgToPicture(string accessToken, DwgFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
其中調用的 TranslateFile()方法如下:
1 /// <summary> 2 /// 發起轉換。 3 /// 源文件上傳成功后,即可發起對該文件的轉換。由於轉換不能立即完成,BIMFace支持在文件轉換完成以后,通過Callback機制通知應用; 4 /// 另外,應用也可以通過接口查詢轉換狀態 5 /// </summary> 6 /// <param name="accessToken">令牌</param> 7 /// <param name="data">請求體數據</param> 8 /// <returns></returns> 9 private FileTranslateResponse TranslateFile(string accessToken, string data) 10 { 11 //PUT https://api.bimface.com/translate 12 string url = BimfaceConstants.API_HOST + "/translate"; 13 14 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 15 headers.AddOAuth2Header(accessToken); 16 17 try 18 { 19 FileTranslateResponse response; 20 21 HttpManager httpManager = new HttpManager(headers); 22 HttpResult httpResult = httpManager.Put(url, data); 23 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 24 { 25 response = httpResult.Text.DeserializeJsonToObject<FileTranslateResponse>(); 26 } 27 else 28 { 29 response = new FileTranslateResponse 30 { 31 Message = httpResult.RefText 32 }; 33 } 34 35 return response; 36 } 37 catch (Exception ex) 38 { 39 throw new Exception("[發起文件轉換]發生異常!", ex); 40 } 41 }
該方法在后面的幾種模型轉換中也會用到,是公用的方法。
其中調用到的 httpManager.Put() 方法,請參考《C# HTTP系列》
請求 body(示例):
{ "source":{ "fileId":857482189666208, "compressed":false, "rootName":"root.dwg" }, "priority":2, "callback":"http://www.app.com/receive", "config":{ "exportDrawing":false // 是否轉成矢量圖紙 } }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。將DWG文件轉換成圖片。 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">DWG文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateDwgToPicture(string accessToken, DwgFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
請求 body(示例):
{ "source":{ "fileId":857482189666208, "compressed":false, "rootName":"root.rvt" // 如果是壓縮文件,必須指定壓縮包中哪一個是主文件 }, "priority":2, "callback":"http://www.app.com/receive", "config":null }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。將RVT文件轉換成着色模式的效果。 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">RVT文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateRvtToRenderStyle(string accessToken, RvtFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
請求 body(示例):
{ "source":{ "fileId":857482189666208, "compressed":false, "rootName":"root.rvt" }, "priority":2, "callback":"http://www.app.com/receive", "config":{"texture":true} // 轉換時是否添加材質 }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。將RVT文件轉換成真實模式的效果。 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">RVT文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateRvtToRealStyle(string accessToken, RvtFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
請求 body(示例):
{ "source":{ "fileId":1402934652281952, "compressed":false }, "priority":2, "config":{ "texture": false, // 轉換時是否添加材質 "exportDwg": true, // rvt2md是否導出dwg文件 "exportPdf": true, // dwg2md是否導出pdf文件 "exportDrawing": true // dwg2md是否導出mdv(矢量化圖紙);取值為true時,exportDwg自動設置為true }, "callback":"http://www.app.com/receive" }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。將RVT格式文件轉換為具備二三維聯動的功能效果。 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">RVT文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateRvtTo23LinkStyle(string accessToken, RvtFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
請求 body(示例):
{ "source":{ "fileId":857482189666208, "compressed":false, "rootName":"root.skp" }, "priority":2, "callback":"http://www.app.com/receive", "config":null }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。其它三維模型文件轉換,常規轉換(不帶材質) 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="request">其他三維模型文件,包括RVT格式文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse TranslateOther3DModelToWithoutMaterialStyle(string accessToken, Other3DModelFileTranslateRequest request) 8 { 9 string data = request.SerializeToJson(); 10 return TranslateFile(accessToken, data); 11 }
請求 body(示例):
{ "source":{ "fileId":1234621112557376, "compressed":true, "rootName":"bimface_2018_打包材質&系統材質庫.rvt" }, "priority":2, "callback":"http://www.app.com/receive", "config":{"texture":true} }
C#實現方法:
1 /// <summary> 2 /// 發起轉換。 3 /// 其他三維模型文件包括RVT格式文件,需要轉換出引用的外部材質場景、貼圖等 4 /// (上傳的文件必須為壓縮包,壓縮包內同級目錄包含模型文件和關聯的所有材質文件,轉換時必須指定rootName為主文件)。 5 /// </summary> 6 /// <param name="accessToken">令牌</param> 7 /// <param name="request">其他三維模型文件,包括RVT格式文件轉化的請求數據對象。根據實際需要設置對象里面的參數,不需要的參數不用賦值</param> 8 /// <returns></returns> 9 public virtual FileTranslateResponse TranslateOther3DModelToWithMaterialStyle(string accessToken, Other3DModelFileTranslateRequest request) 10 { 11 string data = request.SerializeToJson(); 12 return TranslateFile(accessToken, data); 13 }
在BIMFACE的控制台中可以看到我們上傳的文件列表,共計2個文件。

下面以 rac_advanced_sample_project-三維視圖 - From Parking Area.dwg 文件為例,測試“將DWG文件轉換成矢量圖紙”方法。
在如下所示的測試頁面中,DWG文件轉換區域中,選擇相關的轉換參數,然后點擊【將DWG文件轉換成矢量圖紙】按鈕開始轉換

刷新控制台中的列表可以看到該文件的模型狀態顯示為“轉換中”

等待幾秒或者幾分鍾后,該文件的模型狀態顯示為“轉換成功”

待BIMFace轉換完畢后,根據應用傳入的回調地址,BIMFace會通知轉換結果,轉換可能成功、也可能失敗。
查看服務器上配置的Callback處理程序記錄的日志:

Callback的配置項如下:

Callback機制與微信開發需要開發者提供開發者服務器的原理類似,用於開發者與第三方(BIMFACE、微信登)平台進行數據的回傳與邏輯的交互。在ASP.NET開發模式下一般可以采取以下幾種方式來設置:
方式1:使用一般處理程序(.ashx) 處理業務邏輯的交互。
方式2:建立 ASP.NET WebForm 程序,在具體的 WebForm 頁面的構造函數中處理業務邏輯的交互。
方式3:建立 ASP.NET MVC,通過控制器處理業務邏輯的交互。
方式4:建立 ASP.NET 程序,通過 WebAPI 處理業務邏輯的交互。
本示例種采用了第一種方式。
Callback 傳回以下參數:
signature(簽名):為了確保回調消息是由BIMFace發出的,應用在收到回調消息后,須驗證簽名。簽名的計算方式:MD5(``appKey:appSecret:fileId:status:nonce''),如果應用計算的簽名與BIMFace返回的簽名一致,則證明該消息是安全可靠的。
應用收到回調后,須向BIMFace發送回執,回執消息:HTTP STATUS 200
1 /// <summary> 2 /// BimFaceHandler 的摘要說明 3 /// </summary> 4 public class BimFaceHandler : IHttpHandler 5 { 6 public void ProcessRequest(HttpContext context) 7 { 8 context.Response.ContentType = "text/plain"; 9 //context.Response.Write("Hello World"); 10 11 string appKey = ConfigUtility.GetAppSettingValue("BIMFACE_AppKey"); 12 string appSecret = ConfigUtility.GetAppSettingValue("BIMFACE_AppSecret"); 13 string uid = context.Request.QueryString["uid"]; // SparkBimFace 14 15 #region 校驗 16 if (appKey.IsNullOrWhiteSpace()) 17 { 18 LogUtility.Error("BIMFace appKey 配置項沒有配置!"); 19 20 return; 21 } 22 23 if (appSecret.IsNullOrWhiteSpace()) 24 { 25 LogUtility.Error("BIMFace appSecret 配置項沒有配置!"); 26 27 return; 28 } 29 30 if (uid.IsNullOrWhiteSpace()) 31 { 32 LogUtility.Error("[非法請求]回調地址Url鏈接中的參數 uid 沒有配置或者配置的值為空!"); 33 34 return; 35 } 36 #endregion 37 38 long fileId = context.Request.QueryString["fileId"].ToLong(); // 文件ID 39 string status = context.Request.QueryString["status"]; // 轉換的結果 40 string reason = context.Request.QueryString["reason"]; // 若轉換失敗,則返回失敗原因 41 string thumbnail = context.Request.QueryString["thumbnail"]; // 縮略圖地址 42 string nonce = context.Request.QueryString["nonce"]; // 回調隨機數 43 string signature = context.Request.QueryString["signature"]; // BIMFACE的加密簽名 44 45 46 string callbackResponse = string.Format("fileId:{0},\r\nstatus:{1},\r\nreason:{2},\r\nthumbnail:{3},\r\nnonce:{4},\r\nsignature:{5}", 47 fileId, status, reason, thumbnail, nonce, signature); 48 string tip; 49 string custCalcSignature; 50 FileConvertApi api = new FileConvertApi(); 51 bool checkSignature = api.CheckCallbackSignature(appKey, appSecret, fileId, status, nonce, signature, out custCalcSignature); 52 if (checkSignature) 53 { 54 tip = "[BIMFace發出的回調信息簽名驗證成功!]" 55 + Environment.NewLine 56 + callbackResponse; 57 LogUtility.Info(tip); 58 59 //Todo 此處可以根據fileId把相關的信息寫入數據庫中 60 61 // 回執消息:應用收到回調后,須向BIMFace發送回執,回執消息:HTTP STATUS 200 62 context.Response.Write("HTTP STATUS 200"); 63 } 64 else 65 { 66 tip = "[BIMFace發出的回調信息簽名驗證不通過!]" 67 + Environment.NewLine 68 + callbackResponse 69 + Environment.NewLine 70 + "自定義計算簽名 custCalcSignature:" + custCalcSignature; 71 72 LogUtility.Error(tip); 73 74 context.Response.Write(tip); 75 } 76 77 context.Response.End(); 78 } 79 80 /// <summary> 81 /// 該屬性獲得一個布爾值,指示另一個請求是否可以使用該HTTP處理程序的實例。 82 /// <para>如果設置為true,能提高性能,但要注意線程之間安全性問題。如果設置為false,則線程是安全的</para> 83 /// </summary> 84 public bool IsReusable 85 { 86 get 87 { 88 return false; 89 } 90 } 91 }
其中調用到的 CheckCallbackSignature()方法,用於驗證BIMFace發出的回調消息簽名信息是否安全可靠
1 /// <summary> 2 /// 驗證BIMFace發出的回調消息簽名信息是否安全可靠 3 /// </summary> 4 /// <param name="appKey">開發者秘鑰</param> 5 /// <param name="appSecret">開發者密碼</param> 6 /// <param name="fileId">BIMFace發出的回調信息:文件ID</param> 7 /// <param name="status">BIMFace發出的回調信息:轉換的結果</param> 8 /// <param name="nonce">BIMFace發出的回調信息:回調隨機數</param> 9 /// <param name="signature">BIMFace發出的回調信息:簽名</param> 10 /// <param name="custCalcSignature">輸出參數:根據BIMFACE平台的加密規則計算出來的簽名信息</param> 11 /// <returns></returns> 12 public bool CheckCallbackSignature(string appKey, string appSecret, long fileId, string status, string nonce, string signature, out string custCalcSignature) 13 { 14 custCalcSignature = GetCallbackSignature(appKey, appSecret, fileId, status, nonce); 15 16 return custCalcSignature == signature; 17 }
1 /// <summary> 2 /// 獲取BIMFace發出的回調消息簽名信息 3 /// </summary> 4 /// <param name="appKey">開發者秘鑰</param> 5 /// <param name="appSecret">開發者密碼</param> 6 /// <param name="fileId">BIMFace發出的回調信息:文件ID</param> 7 /// <param name="status">BIMFace發出的回調信息:轉換的結果</param> 8 /// <param name="nonce">BIMFace發出的回調信息:回調隨機數</param> 9 /// <returns></returns> 10 public string GetCallbackSignature(string appKey, string appSecret, long fileId, string status, string nonce) 11 { 12 /* signature(簽名):為了確保回調消息是由BIMFace發出的,應用在收到回調消息后,須驗證簽名。 13 * 簽名的計算方式:MD5("appKey:appSecret:fileId:status:nonce"),如果應用計算的簽名與BIMFace返回的簽名一致,則證明該消息是安全可靠的。 14 */ 15 16 return string.Format("{0}:{1}:{2}:{3}:{4}", appKey, appSecret, fileId, status, nonce).EncryptByMD5(); 17 }
這里使用C#的自定義擴展方法 EncryptByMD5()
1 /// <summary> 2 /// 使用 MD5(不可逆加密) 算法加密字符串 3 /// </summary> 4 /// <param name="this">擴展對象。字符串</param> 5 /// <param name="caseType">字符串大小寫。默認小寫</param> 6 /// <returns></returns> 7 public static string EncryptByMD5(this string @this, CaseType caseType = CaseType.Lower) 8 { 9 using (MD5 md5 = MD5.Create()) 10 { 11 var sb = new StringBuilder(); 12 byte[] hashBytes = md5.ComputeHash(Encoding.Default.GetBytes(@this)); 13 foreach (byte bytes in hashBytes) 14 { 15 sb.Append(bytes.ToString("X2"));//X2 表示二進制 16 } 17 18 return caseType == CaseType.Upper ? sb.ToString() : sb.ToString().ToLower(); 19 } 20 }