我正在设置一个新的 Razor Pages 应用程序,并且想要添加基于角色的授权。网络上有很多教程如何使用 ASP.NET MVC 应用程序执行此操作,但没有使用 Razor 页面。我尝试了一些解决方案,但对我来说没有任何作用。目前我有一个问题,如何为数据库添加角色并将该角色添加到每个新注册用户。
这就是我的样子Startup.cs:
public async Task ConfigureServices(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
})
.AddRoles<IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthorization(config =>
{
config.AddPolicy("RequireAdministratorRole",
policy => policy.RequireRole("Administrator"));
});
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.AddRazorPages()
.AddNewtonsoftJson()
.AddRazorPagesOptions(options => {
options.Conventions.AuthorizePage("/Privacy", "Administrator");
}); …Run Code Online (Sandbox Code Playgroud) 我需要使用我的应用程序将一些文件上传到服务器,并且需要提供要保存文件的文件夹的完整路径。我正在使用Path.Combine带有webHostEnvironment.WebRootPath来自 appsettings.json 和文件名的附加文件夹路径的方法。代码如下所示:
var pathString = configuration.GetValue<string>("UploadPaths:Pictures");
var path = Path.Combine(webHostEnvironment.WebRootPath, pathString, picture.Name);
Run Code Online (Sandbox Code Playgroud)
问题是变量中的webHostEnvironment.WebRootPath只是省略了path(路径看起来像 pathString + picture.Name)。
知道如何获得正确的路径吗?
我有一个使用 .net core 3.1 Razor Pages 构建的 Web 应用程序。我需要添加功能以从视图生成 PDF。通常该库正在工作,因为我可以生成静态 PDF,但是当我想使用模型为模板做种时会出现问题。
该PageModel如下所示:
public class DetailsPdfModel : PageModel
{
private readonly ICablesData cablesData;
private readonly IConfiguration configuration;
public DetailsPdfModel(ICablesData cablesData, IConfiguration configuration)
{
this.cablesData = cablesData;
this.configuration = configuration;
}
public Cable Cable { get; set; }
public IActionResult OnGet(int cableId)
{
Cable = cablesData.GetById(cableId);
if (Cable == null)
{
return RedirectToPage("NotFound");
}
else
{
return new ViewAsPdf("DetailsPdf", this);
}
}
}
Run Code Online (Sandbox Code Playgroud)
视图如下所示:
@page
@model DetailsPdfModel
@{
Layout = …Run Code Online (Sandbox Code Playgroud)