.net 如何发送凭据以呼叫服务?
如何进行基本身份验证或将凭据信息发送到服务 我在解决方案中添加了一个服务引用(JAVA服务引用)。当我试图呼叫服务时,它出现了一个错误,说是未经授权的服务请求。呼叫服务时如何发送凭据?在登录窗口中输入用户名和密码后,它应立即发送凭据以呼叫服务。 我是WCF新手,我以前使用过asmx程序。我能这样做吗.net 如何发送凭据以呼叫服务?,.net,wcf,c#-4.0,.net,Wcf,C# 4.0,如何进行基本身份验证或将凭据信息发送到服务 我在解决方案中添加了一个服务引用(JAVA服务引用)。当我试图呼叫服务时,它出现了一个错误,说是未经授权的服务请求。呼叫服务时如何发送凭据?在登录窗口中输入用户名和密码后,它应立即发送凭据以呼叫服务。 我是WCF新手,我以前使用过asmx程序。我能这样做吗 public partial class Program { static void Main(string[] args) { ServiceReferenc
public partial class Program
{
static void Main(string[] args)
{
ServiceReference2.ESIVisServicePortTypeClient client = new ServiceReference2.ESIVisServicePortTypeClient();
ServiceReference2.PartType part = new ServiceReference2.PartType();
part.partnumber = "A2040000049";
ServiceReference2.ResponseType response = client.getPart2D(part);
Console.WriteLine("Major: " + response.major + " Minor: " + response.minor);
}
}
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="VisualizationSoapBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<!--<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="Windows"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>-->
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" realm=""/>
</security>
</binding>
<binding name="VisualizationSoapBinding1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<!--<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="Windows"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>-->
<security mode="None">
<transport proxyCredentialType="Windows" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://vissvc-test.e.corpintra.net:80/VisSvcNative/DAIESIVisService"
binding="basicHttpBinding" bindingConfiguration="VisualizationSoapBinding1"
contract="ServiceReference2.ESIVisServicePortType" name="ESIVisServicePort1" />
</client>
</system.serviceModel>
并获取会话cookie
protected string GetSessionCookie()
{
string ret = "";
WebResponse res;
if (m_webRequest != null)
{
res = base.GetWebResponse(m_webRequest);
if (res != null)
{
for (int i = 0; i < res.Headers.Keys.Count; i++)
{
string key = res.Headers.Keys.Get(i);
string smsession = res.Headers.Get(i);
if (key == ServiceConfiguration.HEADER_COOKIE_NAME_C || key == ServiceConfiguration.HEADER_COOKIE_NAME_SETC || smsession.IndexOf(ServiceConfiguration.COOKIE_SMSESSION) > -1)
{
int index = smsession.IndexOf(ServiceConfiguration.COOKIE_SMSESSION) + ServiceConfiguration.COOKIE_SMSESSION.Length;
// Trim front
smsession = smsession.Substring(index);
index = smsession.IndexOf(ServiceConfiguration.HEADER_COOKIE_SEPARATOR);
// Trim end
ret = smsession.Substring(0, index);
// End loop
//break;
i = res.Headers.Keys.Count;
}
}
}
}
return ret;
}
protected override WebRequest GetWebRequest(Uri uri)
{
//apptoken name to be sent instead of caller app name - 10.1
this.RequestSoapContext.Addressing.From = new Uri(appToken, UriKind.Relative);
WebRequest req = base.GetWebRequest(uri);
req.Headers.Add(ServiceConfiguration.HEADER_COOKIE_NAME_C, ServiceConfiguration.HEADER_COOKIE_VALUE);
req.Method = ServiceConfiguration.REQUEST_METHOD;
req.ContentType = ServiceConfiguration.REQUEST_CONTENT_TYPE;
string smsession = GetSMSessionCookie();
if (smsession != "")
{
req.Headers.Add(ServiceConfiguration.HEADER_COOKIE_NAME_C, smsession);
}
m_webRequest = req;
return req;
}
I am using .net 4.0 C# 2010 winforms.
受保护的字符串GetSessionCookie()
{
字符串ret=“”;
网络反应研究;
if(m_webRequest!=null)
{
res=base.GetWebResponse(m_webRequest);
如果(res!=null)
{
对于(int i=0;i-1)
{
int index=smsession.IndexOf(ServiceConfiguration.COOKIE\smsession)+ServiceConfiguration.COOKIE\smsession.Length;
//正面修剪
smsession=smsession.Substring(索引);
index=smsession.IndexOf(ServiceConfiguration.HEADER\u COOKIE\u分隔符);
//修剪端
ret=smsession.Substring(0,索引);
//端环
//中断;
i=res.Headers.Keys.Count;
}
}
}
}
返回ret;
}
受保护的覆盖WebRequest GetWebRequest(Uri)
{
//要发送的apptoken名称而不是呼叫者应用名称-10.1
this.RequestSoapContext.Addressing.From=新Uri(appToken,UriKind.Relative);
WebRequest req=base.GetWebRequest(uri);
请求头.Add(ServiceConfiguration.HEADER\u COOKIE\u NAME\u C,ServiceConfiguration.HEADER\u COOKIE\u VALUE);
请求方法=服务配置请求方法;
req.ContentType=ServiceConfiguration.REQUEST\u CONTENT\u TYPE;
字符串smsession=getsmsessionokie();
如果(smsession!=“”)
{
请求头添加(ServiceConfiguration.HEADER\u COOKIE\u NAME\u C,smsession);
}
m_webRequest=req;
返回请求;
}
我使用的是.NET4.0C#2010 winforms。
根据这一点,您很可能希望执行以下操作:
client.ClientCredentials.Username.Username=用户名;
client.ClientCredentials.Username.Password=密码
根据您的绑定,该服务没有安全性:
security mode=“None”
您能从SOAP服务的创建者那里找到该服务实际使用的安全性类型吗?@Jonas-这是他们期望使用的模式。因此,该服务中似乎没有内置安全性。再次,ping它们以查看您应该如何发送凭据。也许你需要指定一个令牌,你可以通过调用另一个方法或其他东西来获得它,或者你需要在每次调用时将它们作为参数传递。这是一个SOAP服务还是一个简单的Web服务?@JonasStawski-是的,它的SOAP服务。似乎我们需要先进行基本身份验证,然后将用户名和密码存储在cookie中(使用windows桌面应用程序)。我们对公司自定义门户的用户名和密码进行身份验证,并将其存储在cookie中,然后从Siteminder获取会话cookie,然后将其传递给服务。我们只通过会话(这是一个生成的大令牌)。用户名和密码存储在整个请求中。但是我们发送的每个请求的会话令牌都会更改。请尝试将配置上的安全模式更改为消息,看看是否有效,但只有以相同的方式设置服务时才有效。您确实需要获得一些文档或与服务的创建者交谈。
protected string GetSessionCookie()
{
string ret = "";
WebResponse res;
if (m_webRequest != null)
{
res = base.GetWebResponse(m_webRequest);
if (res != null)
{
for (int i = 0; i < res.Headers.Keys.Count; i++)
{
string key = res.Headers.Keys.Get(i);
string smsession = res.Headers.Get(i);
if (key == ServiceConfiguration.HEADER_COOKIE_NAME_C || key == ServiceConfiguration.HEADER_COOKIE_NAME_SETC || smsession.IndexOf(ServiceConfiguration.COOKIE_SMSESSION) > -1)
{
int index = smsession.IndexOf(ServiceConfiguration.COOKIE_SMSESSION) + ServiceConfiguration.COOKIE_SMSESSION.Length;
// Trim front
smsession = smsession.Substring(index);
index = smsession.IndexOf(ServiceConfiguration.HEADER_COOKIE_SEPARATOR);
// Trim end
ret = smsession.Substring(0, index);
// End loop
//break;
i = res.Headers.Keys.Count;
}
}
}
}
return ret;
}
protected override WebRequest GetWebRequest(Uri uri)
{
//apptoken name to be sent instead of caller app name - 10.1
this.RequestSoapContext.Addressing.From = new Uri(appToken, UriKind.Relative);
WebRequest req = base.GetWebRequest(uri);
req.Headers.Add(ServiceConfiguration.HEADER_COOKIE_NAME_C, ServiceConfiguration.HEADER_COOKIE_VALUE);
req.Method = ServiceConfiguration.REQUEST_METHOD;
req.ContentType = ServiceConfiguration.REQUEST_CONTENT_TYPE;
string smsession = GetSMSessionCookie();
if (smsession != "")
{
req.Headers.Add(ServiceConfiguration.HEADER_COOKIE_NAME_C, smsession);
}
m_webRequest = req;
return req;
}
I am using .net 4.0 C# 2010 winforms.