第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

OWIN 托管的 web api:使用 windows 身份驗(yàn)證并允許匿名訪(fǎng)問(wèn)

OWIN 托管的 web api:使用 windows 身份驗(yàn)證并允許匿名訪(fǎng)問(wèn)

C#
楊__羊羊 2022-12-31 11:28:20
我有一個(gè)WebApi使用OWIN.我想對(duì)某些控制器的操作啟用 Windows 身份驗(yàn)證,但允許匿名調(diào)用其他操作。因此,按照我在網(wǎng)上找到的一些示例,我在課堂上像這樣設(shè)置了我的 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);}然后,在我的控制器中,我創(chuàng)建了兩個(gè)動(dòng)作:[HttpGet][Authorize]public HttpResponseMessage ProtectedAction(){    //do stuff...}[HttpGet][AllowAnonymous]public HttpResponseMessage PublicAction(){    //do stuff...}然而,這是行不通的。調(diào)用標(biāo)記的操作AllowAnonymous按預(yù)期工作,但調(diào)用標(biāo)記的操作Authorize總是返回 401 錯(cuò)誤和以下消息:{    "Message": "Authorization has been denied for this request."}即使調(diào)用者支持 windows 身份驗(yàn)證,在瀏覽器(Chrome 和 Edge)和 Postman 上進(jìn)行了測(cè)試。我在這里錯(cuò)過(guò)了什么?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

好吧,我在另一個(gè)問(wèn)題中找到了解決方法。您可以通過(guò)設(shè)置 AuthenticationSchemeSelector 方法,在運(yùn)行時(shí)為每個(gè)請(qǐng)求選擇身份驗(yàn)證模式,而不是指定多個(gè)身份驗(yàn)證模式(這不起作用):


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;

    }

}

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


查看完整回答
反對(duì) 回復(fù) 2022-12-31
?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

由于您對(duì)問(wèn)題的描述有限,我已經(jīng)設(shè)置了一個(gè)演示應(yīng)用程序,我在其中實(shí)現(xiàn)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在配置類(lèi)中自定義并添加為過(guò)濾器.Filters.Add(new AuthorizeAttribute());


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


它OAuthBearerAuthenticationProvider首先在RequestToken方法中命中,然后在OwinMiddleware類(lèi)中命中,在進(jìn)入任何 DelegatingHandler管道之前,大部分身份驗(yàn)證都是在此層中實(shí)現(xiàn)的。


檢查后請(qǐng)?jiān)u論您的發(fā)現(xiàn),同時(shí)我也修改API并更新您,希望它可以幫助您。


查看完整回答
反對(duì) 回復(fù) 2022-12-31
  • 2 回答
  • 0 關(guān)注
  • 293 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)