基于netsuite suitetalk令牌的身份验证USER_ERROR第二次调用中无效的登录尝试

时间:2020-06-27 16:24:40

标签: netsuite suitetalk

我正在尝试使用Netsuite SuiteTalk https://webservices.netsuite.com/wsdl/v2018_1_0/netsuite.wsdl从.NET调用Netsuite Api。给定一个会话,它可以在first invoke上正常工作,但是会从USER_ERROR Invalid Login Attempt中抛出second invoke。如果我为每个呼叫创建新的会话,则可以正常工作。

using (var service = GetNewSession())
{
    // working
    search(service, code);
    // failed: USER_ERROR Invalid Login Attempt
    search(service, code);
}

GREEN行是第二次调用的地方FAILED。不确定为什么Role列为空。

enter image description here

创建与Netsuite WS的连接

static string accountId = "";
        static string consumerKey = "";
        static string consumerSecret = "";
        static string tokenId = "";
        static string tokenSecret = "";

        public static NetSuiteService GetNewSession()
        {
            // Force TLS 1.2
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            NetSuiteService service = new NetSuiteService();
            service.Timeout = 1000 * 60 * 60 * 2;

            var wsDomainUrl = service.getDataCenterUrls(accountId).dataCenterUrls.webservicesDomain;
            var pathAndQuery = new Uri(service.Url).PathAndQuery;
            var uri = new Uri(wsDomainUrl + pathAndQuery).ToString();
            service.Url = uri;

            service.tokenPassport = CreateTokenPassport();

            return service;
        }

创建令牌护照:

    private static TokenPassport CreateTokenPassport()
    {
        
        string nonce = ComputeNonce();
        long timestamp = ComputeTimestamp();
        TokenPassportSignature signature = ComputeSignature(accountId, consumerKey, consumerSecret, tokenId, tokenSecret, nonce, timestamp);

        TokenPassport tokenPassport = new TokenPassport();
        tokenPassport.account = accountId;
        tokenPassport.consumerKey = consumerKey;
        tokenPassport.token = tokenId;
        tokenPassport.nonce = nonce;
        tokenPassport.timestamp = timestamp;
        tokenPassport.signature = signature;
        return tokenPassport;
    }

计算随机数:

    private static 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 static long ComputeTimestamp()
    {
        return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
    }

计算签名:

    private static TokenPassportSignature ComputeSignature(string compId, string consumerKey, string consumerSecret,
                                    string tokenId, string tokenSecret, string nonce, long timestamp)
    {
        string baseString = compId + "&" + 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;
    }

1 个答案:

答案 0 :(得分:0)

经过Netsuite支持检查后,根据设计,该会话仅可用于1个netsuite呼叫。

相关问题