3 Replies - 259 Views - Last Post: 22 January 2020 - 10:34 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Custom Attribute to take from Body instead of HttpHeaders

Posted 22 January 2020 - 07:14 AM

Hi everyone,

I was recently working on a project and I implemented a custom authorize attribute do authenticate based off a unique value given to the user that expires after a set time period.

The client gets back to me saying that their current technology toolkit is not able to send HTTP headers for a REST service.

My Attribute used the HttpHeaders to get some of those values that I need.

How can I change code in my attribute to take values from the body of an HttpPost request?

This is roughly what I have
 public override void OnAuthorization(HttpActionContext actionContext)
        {
            if (Active)
            {
                try
                {
                    if (string.IsNullOrEmpty(HttpContext.Current.Request.Headers["Guid"]))
                    {
                      // do stuff
                    }
                }
                catch(Exception ex)
                {
                   // do other stuff
                }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Custom Attribute to take from Body instead of HttpHeaders

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,345
  • Joined: 12-December 12

Re: Custom Attribute to take from Body instead of HttpHeaders

Posted 22 January 2020 - 08:12 AM

I'm guessing you should look into FromBody

FromBodyAttribute Class
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 301
  • Joined: 25-May 16

Re: Custom Attribute to take from Body instead of HttpHeaders

Posted 22 January 2020 - 08:38 AM

I'm familiar with [FromBody] but I was not sure if the attribute would accept it as I have never tried that before yet.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,345
  • Joined: 12-December 12

Re: Custom Attribute to take from Body instead of HttpHeaders

Posted 22 January 2020 - 10:34 AM

It is typically an attribute, or annotation?, applied to parameters, rather than to the action as a whole.

As you haven't tried it give it a whirl! And/or read a little more about it's use and purpose.

[I don't know myself whether it addresses your need.]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1