繁体   English   中英

如何从Web服务代码(C#)访问固有的ASP.NET对象?

[英]How do I access intrinsic ASP.NET objects from web service code (C#)?

具体来说,我需要针对数据库验证传入的X.509安全证书,以允许访问我的Web服务。 为此,我需要获得从客户端发送的证书的副本。 我想我知道该怎么做,如果我能弄清楚如何访问http Request对象(一个固有的ASP.NET对象)。

我发现以下代码描述了如何创建将执行此操作的组件,以及如何在aspx页面上的Page Load事件的代码隐藏中加载该组件。 太好了,我从这篇文章中学到了很多东西,但是仍然不能解决我的问题-没有网页,因此没有页面加载事件。

HOW TO:使用Visual C#.NET从.NET组件访问ASP.NET内部对象http://support.microsoft.com/kb/810928

我正在使用C#和.NET 3.5,但没有使用C#的高级编码功能(lambda,扩展方法等)。 我只是没有花时间去学习如何使用它们...

任何提示,指针,示例代码将不胜感激。 谢谢戴夫

如果它是一个asmx Web服务(即“页面” /入口点是somefile.asmx),那么它应该与从那里访问请求对象一样简单。

示例:在Visual Studio中,创建一个Web服务应用程序,然后将以下代码粘贴到service1.asmx.cs中:(下面的示例返回该Web请求中所有标头的名称)

(下面是service1.asmx.cs的完整内容)


using System;
using System.Web;
using System.Web.Services;

namespace WebServiceIntrinsicObjects
{
    /// 
    /// Summary description for Service1
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            // HERE'S THE LINE:  just get the request object from HTTPContext.Current (a static that returns the current HTTP context)
            string test = string.Join(",", HttpContext.Current.Request.Headers.AllKeys);
            return test;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM