簡體   English   中英

使用 ASP.NET Core Identity 時如何限制 ASP.NET Core Web 應用程序模板中的訪問

[英]How is access restricted in the ASP.NET Core web app template when using ASP.NET Core Identity

我有一個使用 ASP.NET Core Identity 進行授權的 ASP.NET Core 5 Web 應用程序。 我已經搭建了所有 Identity UI 的腳手架,以便我可以看到它是如何工作的。

在“身份”區域的腳手架 Razor 頁面中,我可以看到幾個用[AllowAnonymous]修飾的頁面模型類,但我看不到任何對任何限制訪問的引用。

但是,某處肯定有一些東西,因為模板網站中的某些頁面在未登錄時是可訪問的(即使它們沒有[AllowAnonymous] ),但除非登錄,否則腳手架Identity區域中的大多數頁面都無法訪問。

這是如何實現的? 我希望看到對AuthorizeFolder (或AuthorizeAreaFolder )的調用,但我在項目的任何地方都看不到。

我想添加一些我自己的授權規則,但我想在開始更改之前知道現有規則是什么。

要保持對身份 UI 的完全控制,請運行身份腳手架並選擇覆蓋所有文件。

您可能希望這樣做以完全控制身份 UI。

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<IdentityUser, IdentityRole>()
        // services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
            options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        });

    services.ConfigureApplicationCookie(options =>
    {
        options.LoginPath = $"/Identity/Account/Login";
        options.LogoutPath = $"/Identity/Account/Logout";
        options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
    });

    // using Microsoft.AspNetCore.Identity.UI.Services;
    services.AddSingleton<IEmailSender, EmailSender>();
}

參考: 創建完整的 Identity UI 源

ASP.NET Core 中的簡單授權

ASP.NET Core 中的 Razor Pages 授權約定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM