简体   繁体   English

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

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

Specifically, I need to validate the incoming X.509 security certificate against a database to allow access to my web service. 具体来说,我需要针对数据库验证传入的X.509安全证书,以允许访问我的Web服务。 In order to do this, I need to get a copy of the certificate sent from the client. 为此,我需要获得从客户端发送的证书的副本。 I think I know how to do that, if I can figure out how to get access to the http Request object -- an intrinsic ASP.NET object. 我想我知道该怎么做,如果我能弄清楚如何访问http Request对象(一个固有的ASP.NET对象)。

I found the following code that describes how to create a component that will do this, and how to load it in the code-behind for the Page Load event on an aspx page. 我发现以下代码描述了如何创建将执行此操作的组件,以及如何在aspx页面上的Page Load事件的代码隐藏中加载该组件。 That's great, I learned a lot from the article, but it still doesn't address my problem -- there is no web page, so no page load event. 太好了,我从这篇文章中学到了很多东西,但是仍然不能解决我的问题-没有网页,因此没有页面加载事件。

HOW TO: Access ASP.NET Intrinsic Objects from .NET Components by Using Visual C# .NET http://support.microsoft.com/kb/810928 HOW TO:使用Visual C#.NET从.NET组件访问ASP.NET内部对象http://support.microsoft.com/kb/810928

I am using C# and .NET 3.5, but I am not using the advanced coding features of C# (lambdas, extension methods, etc). 我正在使用C#和.NET 3.5,但没有使用C#的高级编码功能(lambda,扩展方法等)。 I just haven't put in the time to learn how to use them yet... 我只是没有花时间去学习如何使用它们...

Any tips, pointers, sample code would be much appreciated. 任何提示,指针,示例代码将不胜感激。 Thanks, Dave 谢谢戴夫

If it's an asmx web service (that is, the "page"/entry point is somefile.asmx, it should be as simple as accessing the request object from there. 如果它是一个asmx Web服务(即“页面” /入口点是somefile.asmx),那么它应该与从那里访问请求对象一样简单。

Example: In Visual Studio, create a web service application, and paste the following code into service1.asmx.cs: (the example below returns names of all of the headers that were in the web request) 示例:在Visual Studio中,创建一个Web服务应用程序,然后将以下代码粘贴到service1.asmx.cs中:(下面的示例返回该Web请求中所有标头的名称)

(below is the complete content of service1.asmx.cs) (下面是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