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

发布于 2024-07-25 20:05:17 字数 283 浏览 8 评论 0原文

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

I am working on integrating a silverlight site into our existing application and am trying to get the login functionality working. The Silverlight application needs to have it's own login page, and the login needs to utilize the existing ASP.NET forms authentication. As part of the login procedure, we are calling some external code, so using the scriptable methods that System.Web.ApplicationServices.AuthenticationService exposes is not an option. I tried to use FormsAuthentication.Authenticate to do this, but it didn't work. Does anyone have any ideas on how to get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2024-08-01 20:05: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)]

It sounds as though you need to create a wrapper websevice which can implement the forms authentication support.

This is something I've done so for example I've created a WCF service with the following interface which is referenced by my Silverlight client:

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

and then in my implementation you can call custom code and also use the forms authentication api, for example to login you could have:

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

Also not you need to include the following attribute above you class definition in your service implementation to have asp.net compat enabled which will give you access to the HttpContext:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
情泪▽动烟 2024-08-01 20:05:17

解决方案很简单。 只需创建一个调用您的自定义代码的自定义会员资格提供程序即可。 有关详细信息,请参阅这篇关于 MSDN 库的文章。 还有 15 秒ASP.NET 网站上的演练视频。 最后,看来微软已经发布了源代码内置会员提供程序

The solution is simple. Just create a custom membership provider that calls your custom code. See this article on MSDN library for more information. There are also full samples available on 15 seconds and a walkthrough video on the ASP.NET website. Finally, it appears Microsoft has released the source for the built-in Membership Provider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文