Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 48097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:13:36+00:00 2026-05-10T16:13:36+00:00

Here’s the situation. I have a webservice (C# 2.0), which consists of (mainly) a

  • 0

Here’s the situation. I have a webservice (C# 2.0), which consists of (mainly) a class inheriting from System.Web.Services.WebService. It contains a few methods, which all need to call a method that checks if they’re authorized or not.

Basically something like this (pardon the architecture, this is purely as an example):

public class ProductService : WebService {     public AuthHeader AuthenticationHeader;      [WebMethod(Description='Returns true')]     [SoapHeader('AuthenticationHeader')]             public bool MethodWhichReturnsTrue()     {         if(Validate(AuthenticationHeader))         {             throw new SecurityException('Access Denied');         }         return true;     }      [WebMethod(Description='Returns false')]     [SoapHeader('AuthenticationHeader')]             public bool MethodWhichReturnsFalse()     {         if(Validate(AuthenticationHeader))         {             throw new SecurityException('Access Denied');         }         return false;     }      private bool Validate(AuthHeader authHeader)     {         return authHeader.Username == 'gooduser' && authHeader.Password == 'goodpassword';     } } 

As you can see, the method Validate has to be called in each method. I’m looking for a way to be able to call that method, while still being able to access the soap headers in a sane way. I’ve looked at the events in the global.asax, but I don’t think I can access the headers in that class… Can I?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:13:36+00:00Added an answer on May 10, 2026 at 4:13 pm

    Here is what you need to do to get this to work correctly.

    It is possible to create your own custom SoapHeader:

    public class ServiceAuthHeader : SoapHeader {     public string SiteKey;     public string Password;      public ServiceAuthHeader() {} } 

    Then you need a SoapExtensionAttribute:

    public class AuthenticationSoapExtensionAttribute : SoapExtensionAttribute {     private int priority;      public AuthenticationSoapExtensionAttribute()     {     }      public override Type ExtensionType     {         get         {             return typeof(AuthenticationSoapExtension);         }     }      public override int Priority     {         get         {             return priority;         }         set         {             priority = value;         }     } } 

    And a custom SoapExtension:

    public class AuthenticationSoapExtension : SoapExtension {     private ServiceAuthHeader authHeader;      public AuthenticationSoapExtension()     {     }      public override object GetInitializer(Type serviceType)     {         return null;     }      public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)     {         return null;     }      public override void Initialize(object initializer)     {             }      public override void ProcessMessage(SoapMessage message)     {         if (message.Stage == SoapMessageStage.AfterDeserialize)         {             foreach (SoapHeader header in message.Headers)             {                 if (header is ServiceAuthHeader)                 {                     authHeader = (ServiceAuthHeader)header;                      if(authHeader.Password == TheCorrectUserPassword)                     {                         return;  //confirmed                     }                 }             }              throw new SoapException('Unauthorized', SoapException.ClientFaultCode);         }     } } 

    Then, in your web service add the following header to your method:

    public ServiceAuthHeader AuthenticationSoapHeader;  [WebMethod] [SoapHeader('AuthenticationSoapHeader')] [AuthenticationSoapExtension] public string GetSomeStuffFromTheCloud(string IdOfWhatYouWant) {   return WhatYouWant; } 

    When you consume this service, you must instantiate the custom header with the correct values and attach it to the request:

    private ServiceAuthHeader header; private PublicService ps;  header = new ServiceAuthHeader(); header.SiteKey = 'Thekey'; header.Password = 'Thepassword'; ps.ServiceAuthHeaderValue = header;  string WhatYouWant = ps.GetSomeStuffFromTheCloud(SomeId); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I haven't had any real problems. That said I have… May 11, 2026 at 9:00 am
  • added an answer I would suggest that your best bet is to install… May 11, 2026 at 9:00 am
  • added an answer You should set attribute userpassword. May 11, 2026 at 9:00 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.