亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OWIN 托管的 web api:使用 windows 身份驗證并允許匿名訪問

OWIN 托管的 web api:使用 windows 身份驗證并允許匿名訪問

C#
楊__羊羊 2022-12-31 11:28:20
我有一個WebApi使用OWIN.我想對某些控制器的操作啟用 Windows 身份驗證,但允許匿名調用其他操作。因此,按照我在網上找到的一些示例,我在課堂上像這樣設置了我的 WebApi Statrup:public void Configuration(IAppBuilder appBuilder){    HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];    listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication | AuthenticationSchemes.Anonymous; //Allow both WinAuth and anonymous auth    //setup routes and other stuff    //...    //Confirm configuration    appBuilder.UseWebApi(config);}然后,在我的控制器中,我創建了兩個動作:[HttpGet][Authorize]public HttpResponseMessage ProtectedAction(){    //do stuff...}[HttpGet][AllowAnonymous]public HttpResponseMessage PublicAction(){    //do stuff...}然而,這是行不通的。調用標記的操作AllowAnonymous按預期工作,但調用標記的操作Authorize總是返回 401 錯誤和以下消息:{    "Message": "Authorization has been denied for this request."}即使調用者支持 windows 身份驗證,在瀏覽器(Chrome 和 Edge)和 Postman 上進行了測試。我在這里錯過了什么?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

好吧,我在另一個問題中找到了解決方法。您可以通過設置 AuthenticationSchemeSelector 方法,在運行時為每個請求選擇身份驗證模式,而不是指定多個身份驗證模式(這不起作用):


public void Configuration(IAppBuilder app)

{

    HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];

            listener.AuthenticationSchemeSelectorDelegate = new 

    AuthenticationSchemeSelector(GetAuthenticationScheme);

}


private AuthenticationSchemes GetAuthenticationScheme(HttpListenerRequest httpRequest)

{

    if(/* some logic... */){

        return AuthenticationSchemes.Anonymous;                    

    }

    else{

        return AuthenticationSchemes.IntegratedWindowsAuthentication;

    }

}

雖然不理想(您必須手動檢查請求 URL 或請求的其他一些參數來決定使用哪種方法)但它可以工作。


查看完整回答
反對 回復 2022-12-31
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

由于您對問題的描述有限,我已經設置了一個演示應用程序,我在其中實現OAuthAuthorizationServerProvider為 Provider forOAuthAuthorizationServerOptions和 override GrantResourceOwnerCredentialsandValidateClientAuthentication


  public void Configuration(IAppBuilder app)

    {

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions

        {

            Provider = new ApplicationOAuthBearerAuthenticationProvider()

        });

        app.Use<AuthenticationResponseMiddleware>();

        var options = new OAuthAuthorizationServerOptions

        {

            AllowInsecureHttp = true,

            TokenEndpointPath = new PathString("/api/xxxx"),

            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 

            Provider = new OwinAuthorisationProvider()


        };

        app.UseOAuthAuthorizationServer(options);


    }

還嘗試AuthorizeAttribute在配置類中自定義并添加為過濾器.Filters.Add(new AuthorizeAttribute());


在AuthenticationResponseMiddleware我繼承OwinMiddleware的public override async Task Invoke(IOwinContext context)方法中,請檢查請求的流程。


它OAuthBearerAuthenticationProvider首先在RequestToken方法中命中,然后在OwinMiddleware類中命中,在進入任何 DelegatingHandler管道之前,大部分身份驗證都是在此層中實現的。


檢查后請評論您的發現,同時我也修改API并更新您,希望它可以幫助您。


查看完整回答
反對 回復 2022-12-31
  • 2 回答
  • 0 關注
  • 280 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號