访问会员提供商后面的Web服务

时间:2010-04-14 15:02:18

标签: c# web-services asp.net-membership

我有一个网站托管使用会员资格提供者授权的网络服务。我希望能够通过我的第二个站点的webservice调用进行身份验证,而不会被退回到我在Web服务站点上的登录页面。我怎么能做到这一点?

作为旁注,我最好为访问该网站的特定主机允许自定义规则。因此,如果请求来自www.mysite.com,它将覆盖授权。

2 个答案:

答案 0 :(得分:1)

您可以在web.config中使用<location/>标记来打开特定目录或文件。

我不相信主机有一种内在的访问控制方法。

一种非常简单的方法是实现一个保护端点或目录的简单HttpModule。

using System;
using System.Net;
using System.Web;

namespace HttpLibArticleSite
{
    public class GuardDogModule : IHttpModule
    {
        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            context.BeginRequest += BeginRequest;
        }

        public void Dispose()
        {
            //noop
        }

        #endregion

        private static void BeginRequest(object sender, EventArgs e)
        {
            HttpContext ctx = (HttpContext) sender;

            if (ctx.Request.Url is not guarded) return;

            if (ctx.Request.Headers["take your pick"] != what
            you want)
            {
                ctx.Response.StatusCode = (int) HttpStatusCode.Forbidden;
                ctx.Response.End();
            }
        }
    }
}

显然没有遵守,也没有经过测试,但会让你到达你需要去的地方。只需参考web.config system.web / httpModules部分即可了解如何部署。

干杯。

答案 1 :(得分:0)

您必须将web.config文件放在Web服务的目录中,并允许匿名访问它。或者您可以只进行Windows身份验证并使用模拟来授权呼叫。