簡體   English   中英

基於 Netsuite 令牌的 API 調用中的模糊身份驗證

[英]Ambiguous Authentication in Netsuite Token Based API call

我正在嘗試使用基於令牌的身份驗證對 Netsuite API 進行 SOAP 調用。 我有一個從 WDSL 生成的 C# 客戶端,它正在發送以下請求(替換了秘密)。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:messages_2016_2.platform.webservices.netsuite.com" xmlns:urn1="urn:core_2016_2.platform.webservices.netsuite.com">
   <soapenv:Header>
      <urn:partnerInfo>
         <urn:partnerId>[MyAccountId]</urn:partnerId>
      </urn:partnerInfo>
      <urn:applicationInfo>
         <urn:applicationId>[MyApplicationId]</urn:applicationId>
      </urn:applicationInfo>
      <urn:tokenPassport>
         <urn1:account>[MyAccountId]</urn1:account>
         <urn1:consumerKey>[MyConsumerKey]</urn1:consumerKey>
         <urn1:token>[MyTokenId]</urn1:token>
         <urn1:nonce>1574515852</urn1:nonce>
         <urn1:timestamp>1499135589</urn1:timestamp>
         <urn1:signature algorithm="HMAC-SHA1">Ll8DbLvTWsBh/G7UtenErR03OrM=</urn1:signature>
      </urn:tokenPassport>
   </soapenv:Header>
   <soapenv:Body>
      <urn:getDataCenterUrls>
         <urn:account>[MyAccountId]</urn:account>
      </urn:getDataCenterUrls>
   </soapenv:Body>
</soapenv:Envelope>

我收到以下回復

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server.userException</faultcode>
         <faultstring>Ambiguous authentication</faultstring>
         <detail>
            <platformFaults:invalidCredentialsFault xmlns:platformFaults="urn:faults_2016_2.platform.webservices.netsuite.com">
               <platformFaults:code>USER_ERROR</platformFaults:code>
               <platformFaults:message>Ambiguous authentication</platformFaults:message>
            </platformFaults:invalidCredentialsFault>
            <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">partners-java20004.sea.netledger.com</ns1:hostname>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

我嘗試了很多不同的方法來生成簽名、隨機數和時間戳。 目前我有以下幾點:

private string computeNonce()
{
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    byte[] data = new byte[20];
    rng.GetBytes(data);
    int value = Math.Abs(BitConverter.ToInt32(data, 0));
    return value.ToString();
}

private long computeTimestamp()
{
    return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}

private TokenPassportSignature computeSignature(string accountId, string consumerKey, string consumerSecret, string tokenId, string tokenSecret, string nonce, long timestamp)
{
    string baseString = accountId + "&" + consumerKey + "&" + tokenId + "&" + nonce + "&" + timestamp;
    string key = consumerSecret + "&" + tokenSecret;
    string signature = "";
    var encoding = new System.Text.ASCIIEncoding();
    byte[] keyBytes = encoding.GetBytes(key);
    byte[] baseStringBytes = encoding.GetBytes(baseString);
    using (var hmacSha1 = new HMACSHA1(keyBytes))
    {
        byte[] hashBaseString = hmacSha1.ComputeHash(baseStringBytes);
        signature = Convert.ToBase64String(hashBaseString);
    }
    TokenPassportSignature sign = new TokenPassportSignature();
    sign.algorithm = "HMAC-SHA1";
    sign.Value = signature;
    return sign;
}

有沒有人有任何想法? 謝謝!

切換到 TBA 后,我也在為這個無益的錯誤而苦苦掙扎。 結果我仍然在發送ApplicationInfo屬性以及新的Consumer KeyConsumer Secret

我在 NetSuite 的“SuiteAnswers”網站上找到了這個,並想在此引用它以供其他仍然遇到此問題的人使用。

不明確的身份驗證錯誤

在 Web 服務中使用基於令牌的身份驗證 (TBA) 時,如果將其他身份驗證機制與 TBA 標頭一起使用,則會返回不明確的身份驗證錯誤響應。

如果除了 TBA 標頭之外,您的請求還包含應用程序 ID、帶有電子郵件地址和密碼的護照對象或有效的 JSESSIONID,您會收到此錯誤。

在以下情況下會發生錯誤:

  • 如果單個 Web 服務請求包含 Passport、TokenPassport 和 SsoPassport 復雜類型的組合。

  • 如果單個 Web 服務請求同時包含 tokenPassport 和 ApplicationInfo 復雜類型,因此在 SOAP 標頭中包含應用程序 ID。

來源: Web 服務中基於令牌的身份驗證錯誤

getDataCenter 調用不需要通行證。 我只是在 mapSso 函數上遇到了同樣的問題。 看起來 2017.1 版本使他們更嚴格地不接受護照

我知道這是一個老問題,但我遇到了同樣的問題,並找到了一個可行的解決方案。

private static void CreateTokenPassport()
{
    // Initialize the netsuite web service proxy.
    _netSuiteService = new NetSuiteService();

    // A valid Token passport signature consists of the following:
    // Create a base string.
    //     The base string is variable created from concatenating a series of values specific to the request.Use an ampersand as a delimiter between values.
    //     The values should be arranged in the following sequence:
    // NetSuite account ID
    // Consumer key
    // Token
    // Nonce(a unique, randomly generated alphanumeric string, with a minimum of six characters and maximum of 64)
    // Timestamp
    // See: https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_4395630653.html#bridgehead_4398049137

    string consumerSecret = "";
    string tokenSecret = "";
    string accountId = "";
    string consumerKey = "";
    string tokenId = "";
    string nonce = ComputeNonce();
    long timestamp = ComputeTimestamp();

    string baseString = string.Format("{0}&{1}&{2}&{3}&{4}", accountId, consumerKey, tokenId, nonce, timestamp);
    string secretKey = string.Format("{0}&{1}", consumerSecret, tokenSecret);

    // Initialize the keyed hash object using the secret key as the key
    HMACSHA256 hashObject = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey));

    // Computes the signature by hashing the data with the secret key as the key
    byte[] signature = hashObject.ComputeHash(Encoding.UTF8.GetBytes(baseString));

    // Base 64 Encode
    string encodedSignature = Convert.ToBase64String(signature);

    TokenPassport tokenPassport = new TokenPassport
    {
        signature = new TokenPassportSignature
        {
            Value = encodedSignature,
            algorithm = "HMAC_SHA256"
        },
        account = accountId,
        consumerKey = consumerKey,
        token = tokenId,
        nonce = nonce,
        timestamp = timestamp
    };

    _netSuiteService = new NetSuiteService
    {
        tokenPassport = tokenPassport
    };
}

實用方法:

private static string ComputeNonce()
{
    return Guid.NewGuid().ToString("N");
}

private static long ComputeTimestamp()
{
    return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}

我不得不修改 XML 並刪除 tokenpassport(account,comsumer key,token,nonce,timestamp) 標簽並且它起作用了。

我不知道它是如何在 C# 中使用 HMAC-SHA1 完成的,但是在使用 CryptoJS HMAC-SHA256 的 Javascript 中,您首先對字符串進行簽名,然后在 Base64 中對其進行編碼:

var baseString = ACCOUNT_ID + "&" + NETSUITE_CONSUMER_KEY + "&" + NETSUITE_TOKEN_ID + "&" + NONCE + "&" + TIMESTAMP;
var key = NETSUITE_CONSUMER_SECRET + '&' + NETSUITE_TOKEN_SECRET;
var HMAC256_Sig = cryptoJS.HmacSHA256(baseString, key);
var HMAC256_Sig_Base64 = cryptoJS.enc.Base64.stringify(HMAC256_Sig);

然后你輸出它:

'<platformCore:signature algorithm = "HMAC_SHA256">' + HMAC256_Sig_Base64 + '</platformCore:signature>'

取出護照。 遺憾的是,如果您在使用令牌身份驗證時在代碼中包含此內容,則 NetSuite 會失敗。 :/

根據 Netsuite SuiteTalk SOAP API 文檔:

不明確的身份驗證錯誤 在 SOAP Web 服務中使用基於令牌的身份驗證 (TBA) 時,如果您將其他身份驗證機制與 TBA 標頭一起使用,則會返回不明確的身份驗證錯誤響應。 如果除了 TBA 標頭之外,您的請求還包含應用程序 ID、帶有電子郵件地址和密碼的護照對象或有效的 JSESSIONID,您會收到此錯誤。 在以下情況下會發生錯誤:

■ 如果單個 SOAP Web 服務請求包含 Passport、TokenPassport 和 SsoPassport 復雜類型的組合。

■ 如果單個 SOAP Web 服務請求同時包含 tokenPassport 和 ApplicationInfo 復雜類型,因此在 SOAP 標頭中包含應用程序 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM