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

时间:2009-07-06 21:37:33

标签: wcf silverlight security forms-authentication membership-provider

我正在努力将silverlight网站集成到我们现有的应用程序中,并尝试使登录功能正常工作。 Silverlight应用程序需要拥有自己的登录页面,登录需要利用现有的ASP.NET表单身份验证。作为登录过程的一部分,我们调用一些外部代码,因此使用System.Web.ApplicationServices.AuthenticationService公开的可编写脚本的方法不是一种选择。我尝试使用FormsAuthentication.Authenticate来做到这一点,但它没有用。有没有人对如何解决这个问题有任何想法?

2 个答案:

答案 0 :(得分:2)

听起来好像你需要创建一个可以实现表单身份验证支持的包装器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)]

答案 1 :(得分:0)

解决方案很简单。只需创建一个调用自定义代码的自定义成员资格提供程有关详细信息,请参阅this article on MSDN library15 secondswalkthrough video on the ASP.NET website还提供完整的示例。最后,对于内置的成员资格提供程序

,它显示为Microsoft has released the source
相关问题