简体   繁体   中英

WCF Membership Provider throws error: content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'

I have a few ASP.NET website and I am trying to set up a WCF service as a membership provider. I want the WCF service to handle all the membership/role details. Logging in/out of one will automatically log in/out of the others, so they will all use this service and I want to keep the code all in one place, rather than each site individually. The service still uses the standard Sql Membership provider.

I get the error in the title when I try to call

Membership.ValidateUser(username, password);

from the website. Is there a way to specify the content type, either on the service or the client?

Server app.config:

<connectionStrings>
  <clear/>
  <add name="LocalSqlServer" connectionString="SQL_Db_With_Membership_info" providerName="System.Data.SqlClient"/>
</connectionStrings>

<system.web>
  <membership defaultProvider="SqlMembershipProvider">
    <providers>
      <clear/>
      <add name="SqlMembershipProvider"
         type="System.Web.Security.SqlMembershipProvider"
         connectionStringName="LocalSqlServer" />
    </providers>
  </membership>
</system.web>

<system.serviceModel>
  <services>
    <service behaviorConfiguration="AspNetMembership"
        name="Login.LoginService">
      <endpoint address="" binding="wsHttpBinding" 
              bindingConfiguration="LoginBinding"
              name="LoginEndpoint"
              contract="Login.ILoginService">
      </endpoint>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8733/Design_Time_Addresses/Membership/MembershipService/" />
        </baseAddresses>
      </host>
    </service>
  </services>

  <behaviors>
    <serviceBehaviors>
      <behavior name="AspNetMembership">
      <serviceCredentials>
        <serviceCertificate findValue="CN=tempCert" />
        <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" 
                                membershipProviderName="SqlMembershipProvider" />
        </serviceCredentials>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <bindings>
    <wsHttpBinding>
      <binding name="LoginBinding">
      </binding>
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

Server Contract:

[ServiceContract]
public interface ILoginService
{
    [OperationContract]
    bool ValidateUser(string username, string password);
}

public class LoginService : ILoginService
{
    public bool ValidateUser(string username, string password)
    {
        return Membership.ValidateUser(username, password);
    }
}

Then on the client side, in the web.config:

<system.web>
  <membership defaultProvider="ClientAuthenticationMembershipProvider">
    <providers>
      <clear/>
      <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="http://localhost:8733/Design_Time_Addresses/Membership/MembershipService/" />
    </providers>
  </membership>
</system.web>

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="LoginEndpoint" />
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:8733/Design_Time_Addresses/Membership/MembershipService/" binding="wsHttpBinding" bindingConfiguration="LoginEndpoint" contract="LoginService.ILoginService" name="LoginEndpoint" >
    </endpoint>
  </client>
</system.serviceModel>

What I want to do is, in the login page code behind, call Membership.ValidateUser to log the user in, like so:

protected void L1_Authenticate(object sender, AuthenticateEventArgs e)
{
    // THIS IS WHERE THE ERROR HAPPENS
    Membership.ValidateUser(L1.UserName, L1.Password);
}

Can I force the server to accept Json or force the client to send XML?

Try adding [WebInvoke] attribute after your [OperationContract] attribute

something like this

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question WCF ERROR: (415) content type 'application/x-www-form-urlencoded' was not the expected type 'application/soap+xml; charset=utf-8' Getting the error Client found response content type of 'text/html; charset=utf-8', but expected 'application/soap+xml'? wcf + The content type text/html of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8) SILVERLIGHT WCF Issue: Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 ' The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8), WCF HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM