20

我正在创建一个 Web Api 应用程序,并且我想使用不记名令牌进行用户身份验证。我按照这篇文章实现了令牌逻辑,一切似乎都运行良好。注意:我没有使用 ASP.NET 身份提供程序。相反,我为它创建了一个自定义用户实体和服务。

 public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        var config = new HttpConfiguration();
        var container = DependancyConfig.Register();
        var dependencyResolver = new AutofacWebApiDependencyResolver(container);
        config.DependencyResolver = dependencyResolver;

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }
}

这是我对 SimpleAuthorizationServerProvider 类的实现

private IUserService _userService;
    public IUserService UserService
    {
        get { return (IUserService)(_userService ?? GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IUserService))); }
        set { _userService = value; }
    }

    public async override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var user = await UserService.GetUserByEmailAndPassword(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);

    }
}

调用 /token url 后,我收到以下错误

从请求实例的范围中看不到带有与“AutofacWebRequest”匹配的标记的范围。这通常表明注册为 per-HTTP 请求的组件正在由 SingleInstance() 组件(或类似场景)请求。在 Web 集成下,始终从 DependencyResolver.Current 或 ILifetimeScopeProvider.RequestLifetime 请求依赖项,而不是从容器本身

有没有办法在这个类中使用依赖注入?我正在使用存储库模式来访问我的实体,因此我认为创建对象上下文的新实例不是一个好主意。这样做的正确方法是什么?

4

3 回答 3

19

我有一个类似的问题。

这里的问题是,当您尝试注入IUserService您的提供程序时,Autofac 检测到它已被注册为InstancePerRequest(使用众所周知的生命周期范围标记'AutofacWebRequest')但是在范围不可见的容器范围内SimpleAuthorizationServerProvider注册。'root''AutofacWebRequest'

建议的解决方案是将依赖项注册为InstancePerLifetimeScope. 这显然解决了问题,但引入了新问题。所有依赖项都在'root'范围内注册,这意味着DbContext所有请求都具有相同的服务实例。史蒂文在这个答案中很好地解释了为什么DbContext在请求之间共享不是一个好主意。

经过更深入的调查任务后,我已经解决了'AutofacWebRequest'从类OwinContext中获取 并从中OAuthAuthorizationServerProvider解决服务依赖项的问题,而不是让 Autofac 自动注入它们。为此,我使用了OwinContextExtensions.GetAutofacLifetimeScope()扩展方法Autofac.Integration.Owin,请参见下面的示例:

using Autofac.Integration.Owin;
...
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
    ...
    // autofacLifetimeScope is 'AutofacWebRequest'
    var autofacLifetimeScope = OwinContextExtensions.GetAutofacLifetimeScope(context.OwinContext);
    var userService = autofacLifetimeScope.Resolve<IUserService>();
    ...
}

我已经以与Laurentiu Stamate在对这个问题的另一个回答中提出的类似方式进行了OAuthAuthorizationServerProvider注册和注入方法,如. 我以同样的方式实施。ConfigureOAuthSingleInstance()RefreshTokenProvider

编辑

@BramVandenbussche,这是我ConfigurationStartup课堂上的方法,您可以在其中看到添加到 OWIN 管道的中间件的顺序:

public void Configuration(IAppBuilder app)
{
    // Configure Autofac
    var container = ConfigureAutofac(app);

    // Configure CORS
    ConfigureCors(app);

    // Configure Auth
    ConfigureAuth(app, container);

    // Configure Web Api
    ConfigureWebApi(app, container);
}
于 2016-04-21T12:27:45.817 回答
12

要在其中使用依赖注入,SimpleAuthorizationServerProvider您必须IOAuthAuthorizationServerProvider像任何其他类型一样注册到 Autofac 容器。你可以这样做:

builder
  .RegisterType<SimpleAuthorizationServerProvider>()
  .As<IOAuthAuthorizationServerProvider>()
  .PropertiesAutowired() // to automatically resolve IUserService
  .SingleInstance(); // you only need one instance of this provider

您还需要将容器传递给ConfigureOAuth方法并让 Autofac 像这样解析您的实例:

var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
    AllowInsecureHttp = true,
    TokenEndpointPath = new PathString("/token"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
    Provider = container.Resolve<IOAuthAuthorizationServerProvider>()
};

如果对象中的属性不通过外部数据更改,则应始终使用单个实例(假设您在控制器中设置了一个属性,该属性依赖于存储在数据库中的某些信息 - 在这种情况下,您应该使用 InstancePerRequest) .

于 2015-08-30T20:29:37.870 回答
3

我还使用 OwinContextExtensions.GetAutofacLifetimeScope 尝试了@jumuro 答案,这可以节省我的时间。这个答案不是在运行时注册 IUserService ,而是提供了一个在请求后验证/创建实例服务的选项。

我添加了一些新答案,因为由于我的声誉低,我还不能发表评论,但添加了额外的指南代码来帮助某人。

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {

        try
        {
            if (service == null)
            {
                var scope = Autofac.Integration.Owin.OwinContextExtensions.GetAutofacLifetimeScope(context.OwinContext);
                service = scope.Resolve<IUserService>();
            }
            var user = await service.FindUserAsync(context.UserName);
            if (user?.HashedPassword != Helpers.CustomPasswordHasher.GetHashedPassword(context.Password, user?.Salt))
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
        }
        catch(Exception ex)
        {
            context.SetError("invalid_grant", ex.Message);
            return;
        }

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));

        AuthenticationProperties properties = CreateProperties(context.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(identity);

    }
于 2016-08-30T06:45:22.307 回答