簡體   English   中英

OWIN承載令牌在無效時無法與授權一起使用

[英]OWIN bearer token not working with Authorize when invalid

我正在扯頭發。 我在登錄期間成功創建了承載令牌。 當我將該令牌傳遞給我的授權api控制器時,一切都會按預期進行。

如果我不傳遞令牌或傳遞無效令牌,則會收到以下錯誤消息:

嘗試創建“ AccountsController”類型的控制器時發生錯誤。 確保控制器具有無參數的公共構造函數。”

我將對象注入控制器中,因此沒有無參數構造函數。 無論如何,是否不應該將未經授權的回復發送回去?

這是我的OWIN設置:

    OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
        Provider = new OauthServerProvider()
    };

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

謝謝保羅

除非使用IoC容器並顯式定義控制器應如何解決依賴關系,否則構建控制器的默認ASP.NET機制需要無參數的構造函數。

解決此問題的簡單方法是創建一個無參數的構造函數,並使用如下參數調用該構造函數:

public TaskController()
    : this(new TaskService(new TaskRepository()))
{

}

public TaskController(ITaskService taskService)
{
    this.taskService = taskService;
}

這里的問題是您的DI不再有用。 您必須在Startup類中配置DI。

我向您展示溫莎城堡的一個例子:

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

    HttpConfiguration config = new HttpConfiguration();
    WebApiConfig.Register(config);
    config.Filters.Add(new WebApiAuthorizeAttribute());

    ...

    // Dependency Resolver
    var container = new WindsorContainer().Install(new WebApiControllersInstaller());
    var httpDependencyResolver = new WindsorDependencyResolver(container);
    config.DependencyResolver = httpDependencyResolver;

    //Uncomment next lines for Composition Root DI configuration intead of Dependency Resolver
    //var container = new WindsorContainer().Install(new WebApiControllersInstaller());
    //config.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(container));

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM