WCF成员身份提供程序引发错误:内容类型';application/json;charset=utf-8';

本文关键字:类型 application utf-8 charset json 身份 成员 程序 错误 WCF | 更新日期: 2023-09-27 18:20:46

我有一些ASP.NET网站,我正在尝试将WCF服务设置为成员资格提供商。我希望WCF服务能够处理所有成员资格/角色的详细信息。登录/注销其中一个会自动登录/注销其他网站,所以他们都会使用这项服务,我想把代码都放在一个地方,而不是每个网站单独保存。该服务仍然使用标准的Sql成员身份提供程序。

当我尝试呼叫时,标题中出现错误

Membership.ValidateUser(username, password);

来自网站。有没有一种方法可以在服务或客户端上指定内容类型?

服务器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>

服务器合同:

[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);
    }
}

然后在客户端,在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>

我想做的是,在后面的登录页面代码中,调用Membership.ValidateUser来登录用户,如下所示:

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

我可以强制服务器接受Json还是强制客户端发送XML?

WCF成员身份提供程序引发错误:内容类型';application/json;charset=utf-8';

尝试在[OperationContract]属性之后添加[WebInvoke]

像这个

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