代码之家  ›  专栏  ›  技术社区  ›  Jeffrey Lott

如何在自定义Web服务中对用户进行表单身份验证?

  •  2
  • Jeffrey Lott  · 技术社区  · 17 年前

    我正在努力将Silverlight站点集成到我们现有的应用程序中,并尝试使登录功能正常工作。Silverlight应用程序需要有自己的登录页,登录需要使用现有的ASP.NET窗体身份验证。作为登录过程的一部分,我们正在调用一些外部代码,因此使用System.Web.ApplicationServices.AuthenticationService公开的可脚本方法不是一个选项。我试图使用FormsAuthentication.Authenticate来进行验证,但没有成功。有人对如何解决这个问题有什么想法吗?

    2 回复  |  直到 17 年前
        1
  •  2
  •   Andy Britcliffe    17 年前

    听起来好像您需要创建一个包装Web设备来实现表单身份验证支持。

    这是我做过的事情,例如,我创建了一个具有以下接口的WCF服务,该接口由我的Silverlight客户端引用:

    [ServiceContract]
        public interface IAuthenticationService
        {
            [OperationContract()]
            string Login(string username, string password, bool isPersistent);
            [OperationContract()]
            bool Logout();
            [OperationContract()]
            string IsLoggedIn();     
        }
    

    然后,在我的实现中,您可以调用自定义代码,还可以使用表单身份验证API,例如,登录时可以使用:

    try
                {
                    //Call you external code here
                    //Then use the membership provider to authenticate
                    if (Membership.ValidateUser(username, password))
                    {
    
                        FormsAuthentication.SetAuthCookie(username, isPersistent);
    
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException("Error in Login", ex);
                }
    

    另外,您也不需要在服务实现中的类定义上方包含以下属性,以便启用ASP.NET Compat,这样您就可以访问httpContext:

     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    
        2
  •  0
  •   Paolo    17 年前

    解决方案很简单。只需创建一个调用自定义代码的自定义成员资格提供程序。见 this article on MSDN library 更多信息。也有完整的样品 15 seconds 和A walkthrough video on the ASP.NET website . 最后,它出现了 Microsoft has released the source 对于内置成员资格提供程序