XAMARIN,WCF,带有自定义用户名/密码和主体

时间:2018-03-26 15:57:48

标签: wcf xamarin

我正在开发一个WCF服务,它将向Xamarin客户端提供数据。我正在尝试使用自定义用户名/密码和自定义原则,以便我可以将服务的可用信息附加到身份。经过多次尝试,我无法在客户端上获得除了不同的错误消息之外的任何内容。我认为问题出在WCF配置中,但我无法弄清问题是什么。我的web.config代码如下。任何有关去哪儿的帮助或提示都将不胜感激!

<system.serviceModel>


<diagnostics wmiProviderEnabled="true">
  <messageLogging
       logEntireMessage="true"
       logMalformedMessages="true"
       logMessagesAtServiceLevel="true"
       logMessagesAtTransportLevel="true"
       maxSizeOfMessageToLog="65535000"
       maxMessagesToLog="3000"
   />
</diagnostics>
<behaviors>
  <serviceBehaviors>
    <behavior name="Behavior">
      <!-- To avoid disclosing metadata information, set the values below 
to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, 
set the value below to true.  Set to false before deployment to avoid 
disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceCredentials>
        <serviceCertificate findValue="Cert" storeLocation="LocalMachine" 
storeName="My" x509FindType="FindBySubjectName"/>
        <userNameAuthentication userNamePasswordValidationMode="Custom" 
customUserNamePasswordValidatorType="Service.ServiceAuthenticator, 
Service"/>
      </serviceCredentials>
      <serviceAuthorization 
serviceAuthorizationManagerType="Service.CustomAuthorizationManager, 
Service" principalPermissionMode="Custom">
        <authorizationPolicies>
          <add policyType="Service.AuthorizationPolicy, Service"/>
        </authorizationPolicies>
      </serviceAuthorization>
      <serviceSecurityAudit
auditLogLocation="Application"
serviceAuthorizationAuditLevel="Failure"

messageAuthenticationAuditLevel="Failure"
suppressAuditFailure="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service name="Service" behaviorConfiguration="Behavior">
    <endpoint address="" binding="basicHttpBinding" 
contract="Service.IService" bindingConfiguration="Secure"/>
    <endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding name="Secure">
      <security mode="Transport">
        <transport clientCredentialType="Basic" 
proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" 
algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
    To browse web app root directory during debugging, set the value 
below to true.
    Set to false before deployment to avoid disclosing web app folder 
information.
  -->
<directoryBrowse enabled="true"/>
<httpErrors errorMode="Detailed" />
</system.webServer>

我在Wamarin解决方案的主项目中使用WFC服务作为连接服务。使用以下代码调用该服务:

        Service.ServiceClient _client;

        _client = new Service.ServiceClient();


        _client.ClientCredentials.UserName.UserName = 
"username";
        _client.ClientCredentials.UserName.Password = "password";

        //To allow service to connect even though certificate was not 
validated.
        ServicePointManager.ServerCertificateValidationCallback = 
MyRemoteCertificateValidationCallback;

        lci = await _client.WCFTestMethod();

更新以下评论的帖子。 Xamarin不支持wsHttpBinding,所以我必须使用basicHttpBinding。当我在IIS站点上将身份验证设置为Anonymous时,我已经执行了checkAccessCore过程,但它会抛出此错误&#34; No Identity Found&#34;当AuthorizationPolicy执行GetClientIdentity时。有没有办法在checkAccessCore中分配一个身份?

1 个答案:

答案 0 :(得分:0)

我能够以我想要的方式使服务工作,确保IIS上的身份验证方法设置为Anonymous,并且在AuthorizationPolicy的Evaluate方法中我创建了GenericIdentity并使用它(下面的示例代码)而不是调用GetClientIdentity方法。该服务现在似乎正在运行,并且正在checkAccessCore方法中对用户进行身份验证。

    Dim client As IIdentity = New GenericIdentity("User")

    Dim cp As CustomPrincipal = New CustomPrincipal(client)

    evaluationContext.Properties("Identities") = l


    evaluationContext.Properties("Principal") = cp