Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

前端 未结 3 1872
陌清茗
陌清茗 2020-12-15 18:24

I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to w

3条回答
  •  执念已碎
    2020-12-15 19:00

    To use dependency injection in SimpleAuthorizationServerProvider you have to register IOAuthAuthorizationServerProvider to the Autofac container just like any other type. You can do something like this:

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

    You also need to pass the container to the ConfigureOAuth method and let Autofac resolve your instance like this:

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

    You should always use single instances if your properties within the object don't change via external data (let's say you have a property which you set in the controller which dependents upon some information stored in the database - in this case you should use InstancePerRequest).

提交回复
热议问题