I have following code:
public class ContaController : ApiController
{
private readonly IAppServiceUsuario _app;
public ContaController(IAppServiceUsuario _app)
{
this._app = _app;
}
}
How to inject dependency with Ninject
using that same IAppServiceUsuario
in another class that is not a Controller
?
This is mapped to Ninject
:
kernel.Bind<IAppServiceUsuario>().To<AppServiceUsuario>();
Class where you should instantiate IAppServiceUsuario
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private IAppServiceUsuario _app;
public AuthorizationServerProvider(IAppServiceUsuario _app)
{
this._app = _app;
}
/// <summary>
/// Método para validar o token no cache do Oauth
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
return Task.FromResult<object>(null);
}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
context.AdditionalResponseParameters.Add("User", "");
return Task.FromResult<object>(null);
}
/// <summary>
/// Metodo para verificar as credenciais de acesso
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
if (_app.Autenticar(context.UserName, context.Password) != null)
{
Claim claim1 = new Claim(ClaimTypes.Name, context.UserName);
Claim[] claims = new Claim[] { claim1 };
ClaimsIdentity claimsIdentity =
new ClaimsIdentity(
claims, OAuthDefaults.AuthenticationType);
context.Validated(claimsIdentity);
}
else
{
context.SetError("Erro na validação", "Usuario ou senha Inválidos");
}
return Task.FromResult<object>(null);
}
}