【问题标题】:C# Consuming web service wsdl with access tokenC# 使用带有访问令牌的 Web 服务 wsdl
【发布时间】:2022-01-19 00:36:36
【问题描述】:

我正在尝试使用 WSDL Web 服务,它需要在标头中发送访问令牌。但是,我不断收到 401 错误,我不确定我是否正确注入了令牌。

这是代码的sn-p:

 var client = new WsldClient(); 
 var operationContext = new OperationContext(client.InnerChannel); 
 using (new OperationContextScope(operationContext))
 {
     var httpRequestProperty = new HttpRequestMessageProperty();
     httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Bearer " + accessToken
     operationContext.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

     client.SomeMethod();
 }

这会返回 401 错误。

【问题讨论】:

    标签: c# web-services wcf wsdl access-token


    【解决方案1】:

    你可以试试下面的代码:

    var client = new MyClient();
    client.ClientCredentials.UserName.UserName = "username";  
    client.ClientCredentials.UserName.Password = "password";
    var httpRequestProperty = new HttpRequestMessageProperty(); 
    httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +   Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
    var context = new OperationContext(ormClient.InnerChannel); 
    using (new OperationContextScope(context)) 
    {
          context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
          return await client.SomeMethod(); 
    }
    

    Authorization Header is missing in Http request using WCF
    http://plainoldstan.blogspot.com/2008/07/avoid-http-401-roundtrip-with-adding.html

    【讨论】:

    • 我可以试试,但是 wcf 服务需要访问令牌
    • 希望this post能帮到你。
    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多