Custom Authorization - How do I use data retrieved later in the controller?http://forums.asp.net/t/1771313.aspx/1?Custom+Authorization+How+do+I+use+data+retrieved+later+in+the+controller+Tue, 28 Feb 2012 21:08:53 -050017713134839940http://forums.asp.net/p/1771313/4839940.aspx/1?Custom+Authorization+How+do+I+use+data+retrieved+later+in+the+controller+Custom Authorization - How do I use data retrieved later in the controller? <p>I am using a Custom Authorization Attribute for an MVC Web Service that takes JSON and / or Xml payloads.&nbsp; Part of each payload is some data that identifies the customer that is submitting the API.&nbsp; I want to use the custom attribute to pull data out of the payload and verify that the customer is authenticated and authorized to attempt the requested API.&nbsp; This is done by calling some methods in my repository that check the database against the credentials sent in the payload and then returning a TRUE or FALSE.&nbsp; However, because I hit the database and gathered a lot of good info, I want to be able to keep this for the duration of the request processing.&nbsp; This customer data has special config data for this customer as well as other unique info that the rest of the app needs to properly service the request for this specific customer.</p> <p>I do not want to hit the DB again, since I did it at authorization time.&nbsp; I am replacing an older WCF service that did this just fine, but then it was part of my API logic as we did not have these cool attributes.</p> <p>My questions are two:</p> <p>1) How can I get the &quot;object&quot; that is passed in as part of the request in the authorizaiton code so I can actually perform the required auth?</p> <p>2) Once it &quot;passes&quot;, I get into my controller code, so how do I pass the values that I retrived from the DB from the authorization attribute code into my controller action where I actually need them.</p> <p>These are totally stateless API calls (as they should be for an API), so each request is authenticated and I need the CustomerInformation object that I built in Authorization to be available in my controller action.</p> <p>Any help is appreciated - I am using the BETA MVC 4 Web API.</p> <p>&nbsp;</p> 2012-02-19T20:33:54-05:004855590http://forums.asp.net/p/1771313/4855590.aspx/1?Re+Custom+Authorization+How+do+I+use+data+retrieved+later+in+the+controller+Re: Custom Authorization - How do I use data retrieved later in the controller? <p>Figured this out. &nbsp;First of all, the AuthorizeAttribute that you want to override is the&nbsp;System.Web.Http.AuthorizeAttribute not the&nbsp;System.Web.Mvc.AuthorizeAttribute.</p> <p>Once I did this, the OnAuthorization override is the place to put the code.</p> <p>you need to add another attribute to your specialized authorization to AllowAnonymous - again be sure to use the System.Web.HttpAllowAnonymous, not the MVC one.</p> <p>Then you can access your controller class using:</p> <p>MyController c = actionContext.ControllerContext.Controller as MyController;</p> <p>Then you can set any propery on that class - this is where you put your info that you painfully extracted from the DB while authorizing this request.</p> <p>Then, if you want to fail the request because it did not pass your strict rules. simply set the actionContextResponse like this:</p> <p>actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);</p> <p>otherwise just fall through and you will be in your Controller Method with the pretty little property you set earlier all intact.</p> <p>By the way - do not call the base.OnAuthorization because it will fail you because some membership provider is not there....</p> <p>The big problem I had was that I used the MVC versions of the attributes, not the http versions.</p> <p>Code snippet of my custom attribute is here: -- Hope this helped someone!</p> <pre class="prettyprint">[AttributeUsage(AttributeTargets.Method, Inherited=true, AllowMultiple=true)] [System.Web.Http.AllowAnonymous] public class AuthorizeMerchantAttribute : System.Web.Http.AuthorizeAttribute { public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) { // // bypass the base class code // //base.OnAuthorization(actionContext); // // get a reference to my controller // TestAuthController c = actionContext.ControllerContext.Controller as TestAuthController; // // Call some method to do my custom authorization setting a property on my controller to the results // c.MerchantInformation = ValidateMerchant(); // // Check to see if my magic boolean is FALSE, and if so, return Unauthorized // if (!c.MerchantInformation.IsAllowed) { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); } } }</pre> 2012-02-28T21:08:53-05:00