我按照這篇文章使用Identity Server 4建立Authorization Server建立了IdentityServer服務器。然后直接建了一個Web服務器,我沒有使用文中提到的 [Authorize] 標簽。自定義了一個認證篩選器。 public class IdentityFilter : IAuthorizationFilter
{ public void OnAuthorization(AuthorizationFilterContext context)
{if (!context.HttpContext.User.Identity.IsAuthenticated)
{
Console.WriteLine("need login");
} }
} 注冊為全局篩選器,在ConfigureServices中添加 services.AddMvc(option => {
option.Filters.Add(typeof(IdentityFilter));
}); 篩選器可以正常過濾請求,控制臺也能根據票據狀態輸出文字。然后我新建了一個控制器,給控制器加上 [AllowAnonymous] 屬性 [AllowAnonymous] public class HomeController : Controller
{ public IActionResult Index()
{ return View();
}
} 感覺按照Framework套路應該這里就可以不用參與認證過濾但是通過調試發現,這里依舊在輸出文字。 請問,大神這個是Core和Framework的區別么?還是我的操作有問題?
- 0 回答
- 0 關注
- 731 瀏覽
添加回答
舉報
0/150
提交
取消