AdminController中添加引用: Index添加[Authorize]許可權要求: StartUp.cs中添加引用: StartUp.cs ConfigureServices中添加常量: 添加中間件: 此時,訪問Admin頁面,自動跳轉至Account/Login?ReturnUrl=%2F ...
AdminController中添加引用:
1 using Microsoft.AspNetCore.Authorization;
Index添加[Authorize]許可權要求:
1 [Authorize] 2 public IActionResult Index() 3 { 4 return View(); 5 }
StartUp.cs中添加引用:
1 using Microsoft.AspNetCore.Authentication; 2 using Microsoft.AspNetCore.Authentication.Cookies;
StartUp.cs ConfigureServices中添加常量:
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(); 4 services.AddMvc(); 5 }
添加中間件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
}
此時,訪問Admin頁面,自動跳轉至Account/Login?ReturnUrl=%2FAdmin