c# - 如何更改.Net core中的访问拒绝路由

标签 c# asp.net-core asp.net-core-identity

我在角色和路线方面遇到问题 我有一个帐户 Controller ,并且我在其中一个 Controller 中使用 Asp Identity。它需要特定的角色才能访问它。问题是,我希望它将未经授权的用户重定向到登录页面“帐户/登录”,但它却将我重定向到“帐户/访问拒绝”

我尝试了以下方法,但没有成功

 services.AddIdentity<AppUser, AppRole>(option =>
           {
               option.User.RequireUniqueEmail = true;
               
           }).AddEntityFrameworkStores <AppIdentityDbContext>(); 

这里我正在编辑路径

services
        .AddAuthentication()
        .AddCookie(options =>
        {
            options.LoginPath = "account/login";
            options.LogoutPath = "account/logout";
            options.AccessDeniedPath = "account/login";
            
        });

这是 Controller

[Authorize(Roles = "admin , secretary")]

最佳答案

你可以使用

        services.ConfigureApplicationCookie(options =>
        {

            options.LoginPath = "/Identity/Account/Login";  //in your case /Account/Login
            options.LogoutPath = "/Identity/Account/logout";
            options.AccessDeniedPath = "/Identity/Account/login";
        });

        services.AddAuthentication().AddCookie();

此外,如果您正在使用 .net core 1.x,并希望迁移到 2.0 或更高版本,请按照此 Microsoft migration article 操作。

关于c# - 如何更改.Net core中的访问拒绝路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67339903/

相关文章:

c# - 下拉菜单中的回发或回调参数无效

c# - 输入类型 ="hidden"asp-for= 不适用于 List

asp.net-core - vNext 与 ASP.NET 5

C# ASP.NET Core [Inject] 依赖注入(inject)的属性用法

asp.net-core - .NET Core 2.1 MVC Identity Authorization - 不同部分的不同用户角色

c# - 使用 C# 将 sql 查询的值分配给 CRM 中的字段

c# - 如何从单元测试中引用 VS 2012 可移植类库?

c# - 不可检测的内存泄漏

c# - 共享服务器上 PersistKeysToFileSystem 的文件路径

c# - ArgumentNullException : Value cannot be null.(参数 'items')异常