我有一个承载WCF服务并使用集成Windows身份验证的ASP.net应用程序,我正在尝试在Xamarin Android应用程序中使用此服务。
IIS中的应用程序身份验证设置配置如下。
服务端的Web.config定义服务设置如下
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="HttpWindowsBinding"
maxReceivedMessageSize="2147483647">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="EmployeeServicesWCF">
<endpoint address="http://localhost:801/CSE/EmpServices/EmployeeServicesWCF.svc"
binding="HttpWindowsBinding"
contract="CivilServicesEmpProj.IEmployeeServicesWCF" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
在客户端(Xamarin Android应用程序)的事情安排如下
使用SvcUtil.exe生成代理类并将类添加到项目后,我使用以下代码来使用该服务。
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
EndpointAddress endpoint = new EndpointAddress("http://10.0.2.2:801/CSE/EmpServices/EmployeeServicesWCF.svc");
EmployeeServicesWCFClient client = new EmployeeServicesWCFClient(binding, endpoint);
client.ClientCredentials.UserName.UserName = "10.0.2.2\\MyUser";
client.ClientCredentials.UserName.Password = "mypass";
Employee emp = client.GetEmployeeInfo();
当我运行代码时,我在EmployeeDataServicesWCFClient类的GetEmployeeInfo()方法定义中存在的唯一单行中得到此异常“System.NullReferenceException:对象引用未设置为对象的实例”,请参见下文
public CSEDataContracts.Employee GetEmployeeInfo()
{
return base.Channel.GetEmployeeInfo(user);
}
值得一提的是,当我将上述代码中的身份验证凭据更改为错误凭据时,我得到此异常“System.Net.WebException:处理Web请求时出错:状态代码401(未授权):未经授权”< / p>
知道可能是什么问题吗?