简体   繁体   中英

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. 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.

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. 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

I am using C# and .NET 3.5, but I am not using the advanced coding features of C# (lambdas, extension methods, etc). 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.

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)

(below is the complete content of 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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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