C# 使用依赖项注入unity配置的简单授权服务提供程序
我正在开发一个应用程序,希望在其中使用依赖项注入将IAuthrepository注入SimpleAuthorizationServiceProvider。但当我运行应用程序时,它会返回null异常。这是我的密码C# 使用依赖项注入unity配置的简单授权服务提供程序,c#,asp.net-web-api,dependency-injection,asp.net-web-api2,unity-container,C#,Asp.net Web Api,Dependency Injection,Asp.net Web Api2,Unity Container,我正在开发一个应用程序,希望在其中使用依赖项注入将IAuthrepository注入SimpleAuthorizationServiceProvider。但当我运行应用程序时,它会返回null异常。这是我的密码 public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwl
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
HttpConfiguration config = new HttpConfiguration();
UnityConfig.Register(config);
WebApiConfig.Register(config);
ConfigureOAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider((IAuthRepository)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IAuthRepository)))
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
下面是SimpleAuthorizationServiceProvider类
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private IAuthRepository _repo;
public SimpleAuthorizationServerProvider(IAuthRepository repo)
{
_repo = repo;
}
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
if (context.ClientId == null)
{
context.Validated();
}
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
try
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
当我运行应用程序时,\u repo始终为null,因此它会给我null异常。这里是注册存储库
public static void Register(HttpConfiguration config)
{
var container = new UnityContainer();
container.RegisterType<IAuthRepository, AuthRepository>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
}
公共静态无效寄存器(HttpConfiguration配置)
{
var container=new UnityContainer();
RegisterType(新的层次结构CallifetimeManager());
config.DependencyResolver=新的UnityResolver(容器);
}
您尝试从静态全局配置中解析,但用于注册依赖项的HttpConfiguration
是您在配置方法中手动创建的实例
重构以使用本地配置实例
public void Configuration(IAppBuilder app) {
HttpConfiguration config = new HttpConfiguration();
UnityConfig.Register(config);
WebApiConfig.Register(config);
ConfigureOAuth(app, config.DependencyResolver);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app, IDependencyResolver resolver) {
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() {
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider((IAuthRepository)resolver.GetService(typeof(IAuthRepository)))
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
您试图从全局配置中解析,但用于注册的配置是您在配置
方法中手动创建的配置