Note that there are some explanatory texts on larger screens.

plurals
  1. PONinject and Asp.Net Web API problems with dependency injection
    primarykey
    data
    text
    <p>I have an ASP.NET web api project side by side with a MVC3 project. For both projects Ninject is configured as a dependency resolver.</p> <p>I'm having problems in the web api project, since I am writing my own Authorization attribute, in order to authorize incoming requests to my API. In this attribute, I need to inject some dependencies to perform some verifications on the request.</p> <p>So this is how the code in the attribute looks:</p> <pre><code>public class CustomAuthorizeAttribute : ActionFilterAttribute { public string Roles { get; set; } [Inject] public IAuthSchemaSelector _schemaSelector { private get; set; } [Inject] public IUserService _userService { private get; set; } [Inject] public IRoleVerifier _roleVerifier { private get; set; } public override void OnActionExecuting(HttpActionContext actionContext) { try { var principal = _schemaSelector.Authorize(actionContext.Request, Roles, _userService, _roleVerifier); actionContext.Request.Properties["MS_UserPrincipal"] = principal; } catch (Exception ex) { //create error response with proper http status } } } </code></pre> <p>}</p> <p>_schemaSelector decides which authorization schema the client is using to request services contained in the API (this is because I'm planning to support different schemas like OAuth, API Key authorization, etc. Right now I'm just using HttpBasic). So if eveythign goes ok, i set the principal user, so i can have access to it in the ApiController.</p> <p>Everything goes perfect, until I make modifications from de MVC3 project, the web site. For example, there is a feature in the web site which takes care of changing the user password. Suppose you have an initial password 123456. I make a request from the api using HttpBasic, api responds http status 202.</p> <p>Now I change my password from the web site to 1234567, perform another request from the api using the old password (123456), and it responds 202, which is so wrong.</p> <p>The _userService object is not behaving properly (and neither the other two objects injected), when i debug the action filter, i notice that the whole object is in a previous state. Im guessing the is something wrong on how ninject is configured.</p> <p>This is how I configured ninject:</p> <p>In NinjectWebCommon.cs:</p> <pre><code>GlobalConfiguration.Configuration .ServiceResolver .SetResolver(t =&gt; kernel.TryGet(t), t =&gt; kernel.GetAll(t)); </code></pre> <p>in the ninject module file:</p> <pre><code>kernel.Bind&lt;IUserService&gt;().To&lt;UserService&gt;(); //user service is part of the unit of work kernel.Bind&lt;IUnitOfWork&gt;().To&lt;EFUnitOfWork&gt;().InRequestScope(); kernel.Bind&lt;IRoleVerifier&gt;().To&lt;RoleVerifier&gt;(); kernel.Bind&lt;IAuthSchemaSelector&gt;().To&lt;AuthSchemaSelector&gt;(); </code></pre> <p>It's worth mention that I'm injecting _userService into the ActionFilterAttribute using the property injection pattern, since i can't use constructor pattern. I dont know if this is the problem.</p> <p>Please help! I'm kind of lost here!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload