3
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 a class other than one Controller
?
It is mapped like this in Ninject
:
kernel.Bind<IAppServiceUsuario>().To<AppServiceUsuario>();
Class that should be instantiated 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);
}
}
Eduardo, put this class and how it is instantiating, please?
– novic
added the class
– Eduardo Sampaio
Eduardo already tried to put in Ninject
kernel.Bind<OAuthAuthorizationServerProvider>().To<AuthorizationServerProvider >();
to see if it works? you just forgot to mention how you’re instilling this? or this is the app that does?– novic
Iappserviceusuario instancia ha Appserviceusuario
– Eduardo Sampaio
Yes Eduardo that I understood, but, as it passed to the builder of another class the Ninject needs to know to inject in her also so I asked Eduardo already tried to put in the Ninject kernel. Bind<Oauthauthorizationserverprovider>(). To<Authoriza tionServerProvider >();?
– novic
so I couldn’t
– Eduardo Sampaio