要求登录的解决方法可以通过在ASP.NET Core 3中使用AuthorizeAttribute或页面管理来实现。下面是一个包含代码示例的解决方法:
使用AuthorizeAttribute:
[Authorize]
public class HomeController : Controller
{
// Controller actions...
}
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login"; // 设置登录路径
options.AccessDeniedPath = "/Account/AccessDenied"; // 设置无权限访问路径
});
services.AddAuthorization();
// ...
}
使用页面管理:
@attribute Microsoft.AspNetCore.Authorization.Authorize。@attribute Microsoft.AspNetCore.Authorization.Authorize
Secure Page
Secure Page
This page requires authentication.
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddRazorPages()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/SecurePage"); // 要求登录的页面
});
// ...
}
这些方法可以帮助你在ASP.NET Core 3中实现要求登录的功能。请根据你的具体需求选择其中之一。