【问题标题】:Custom ASP.NET Forms Authentication Service with WCF使用 WCF 自定义 ASP.NET 表单身份验证服务
【发布时间】:2010-10-04 15:40:19
【问题描述】:

我正在尝试使用 WCF 创建自定义 ASP.NET 表单身份验证服务。我通过仅包含一行 JS(ScriptManager 脚本除外)的测试页面调用它。问题是服务器返回响应代码 500 并且响应正文为空。我在服务方法和 Global.asax 中的 Application_Error 中的断点没有被命中。

Sys.Services.AuthenticationService.login('user', 'pass', false, null, null, null, null, null);

我可以在浏览器工具中看到请求转到服务器,请求正文如下:

{"userName":"user","password":"pass","createPersistentCookie":false}

请求方面的其他事情似乎也很好。

这里是配置服务:

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="BtxAuthenticationEndpointBehavior">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="MyNamespace.BtxAuthenticationService">
      <endpoint contract="MyNamespace.IBtxAuthenticationService" binding="webHttpBinding" behaviorConfiguration="BtxAuthenticationEndpointBehavior"/>
    </service>
  </services>
</system.serviceModel>

以及接口的声明:

[ServiceContract]
public interface IBtxAuthenticationService
{
    [OperationContract]
    [WebInvoke]
    bool Login(string username, string password, bool createPersistentCookie);

    [OperationContract]
    [WebInvoke]
    void Logout();
}

实现:

public class BtxAuthenticationService : IBtxAuthenticationService
{
    public bool Login(string username, string password, bool createPersistentCookie)
    {
        ... irrelevant because this is never hit
    }

    public void Logout()
    {
    }
}

谁能告诉我如何配置它或指出一种调试它的方法。一篇关于使用 WCF 服务实现自定义表单身份验证的文章也将受到欢迎。我尝试了各种其他设置,包括我能找到但无法取得任何进展的所有异常详细信息设置(尽管我能够进行一些回归并获得不同的异常,例如缺少端点等)。

感谢您的宝贵时间。

【问题讨论】:

    标签: wcf asp.net-authentication


    【解决方案1】:

    不确定这是否有帮助。我从来没有写过这样的服务,但是你的配置创建了 WCF 服务,它不是 ASP.NET AJAX 准备好的,并且可以使用 XML 而不是 JSON。尝试使用这个而不是 webHttp 行为:

    <endpointBehaviors> 
      <behavior name="BtxAuthenticationEndpointBehavior"> 
        <enableWebScript /> 
      </behavior> 
    </endpointBehaviors>  
    

    【讨论】:

    • 我已经试过了。明天当我到达我的工作机器时我会再试一次,但我相信 enableWebScript 负责提供在这种情况下不需要的 JS 代理。我可能对此有误,但即使这是问题所在,我也应该能够看到一些更具描述性的错误。
    • 是的,你是对的。 EnableWebScript 允许添加服务作为对 ScriptManager 的引用并提供 JS 代理。但它也将消息格式切换为 JSON。您还可以通过设置请求和响应格式在 WebInvoke 属性中执行此操作。同时尝试打开 AspNetCompatibility 级别。
    • 我尝试通过 webHttp 元素设置格式(它有一个属性)但这没有帮助。
    • 您可以尝试开启 WCF 跟踪并检查 WCF 内部错误:msdn.microsoft.com/en-us/library/ms733025.aspx
    • 你是对的。仅添加 enableWebScript 解决了该问题。我曾多次尝试添加 webHttp 和 enableWebScript,但它不起作用。我应该停止尝试猜测 WCF 配置选项的作用,并阅读有关它的内容。我确信我只尝试了 enableWebScript,但我再次尝试只是为了确保它有效。非常感谢。
    猜你喜欢
    • 2011-06-24
    • 2011-02-23
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2010-11-20
    • 2014-09-28
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多