要使用ASP.NET Core身份验证作为UI的基本URL,可以按照以下步骤进行设置:
首先,确保已经安装了Microsoft.AspNetCore.Authentication.Cookies
NuGet包。
在Startup.cs
文件中的ConfigureServices
方法中,添加以下代码来配置身份验证服务:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login"; // 设置登录页面的URL
options.AccessDeniedPath = "/Account/AccessDenied"; // 设置访问被拒绝页面的URL
});
Configure
方法中,添加以下代码来启用身份验证中间件:app.UseAuthentication();
[Authorize]
属性进行标记,例如:[Authorize]
public class HomeController : Controller
{
// ...
}
[Authorize]
属性来限制访问,例如:@{
ViewData["Title"] = "Home Page";
}
Welcome, @User.Identity.Name!
This is a secure page that requires authentication.
这样,只有经过身份验证的用户才能访问被标记为[Authorize]
的Controller和视图。
希望以上解决方法能够帮助到你!