C# 基于角色的授权属性在ASP.NET核心MVC中不起作用
我正在尝试在ASP.NET Core MVC中构建一个应用程序,并使用基于角色的授权和windows身份验证 即使我已经在我的基本控制器上添加了HttpContext中的所有角色,但当导航到我的帐户没有所需角色的控制器时,它仍然不会抛出未授权提示/错误 我认为唯一的问题可能是我没有在Startup.cs正确配置授权 以下是我的启动配置服务:C# 基于角色的授权属性在ASP.NET核心MVC中不起作用,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我正在尝试在ASP.NET Core MVC中构建一个应用程序,并使用基于角色的授权和windows身份验证 即使我已经在我的基本控制器上添加了HttpContext中的所有角色,但当导航到我的帐户没有所需角色的控制器时,它仍然不会抛出未授权提示/错误 我认为唯一的问题可能是我没有在Startup.cs正确配置授权 以下是我的启动配置服务: public void ConfigureServices(IServiceCollection services) { services.AddD
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CimsDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));
services.AddControllersWithViews();
services.AddDistributedMemoryCache();
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddAuthorization();
services.AddSingleton(Configuration);
services.AddSession(options =>
{
options.Cookie.Name = ".MySession";
options.IdleTimeout = TimeSpan.FromMinutes(Convert.ToDouble(Configuration.GetSection("inMinutes").Value));
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
然后我的基本控制器类:
[AllowAnonymous]
public class AppSessionController : Controller
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.User.Identity.IsAuthenticated)
{
//no valid session / create new session
if (String.IsNullOrEmpty(HttpContext.Session.GetString(AppSessionIdentifier.IsAlive)))
{
//initialize new user identity
HttpContext.User = UserIdentity.NewUserIdentity(config, HttpContext);
}
}
base.OnActionExecuting(filterContext);
}
}
接下来是我的NewUserIdentity函数:
public static GenericPrincipal NewUserIdentity(IConfiguration config, HttpContext httpContext)
{
string AccountName = httpContext.User.Identity.Name.Split('\\')[1];
string Env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
List<string> roles = new List<string>();
if (ADHelper.IsInGroup(AccountName, config.GetSection($"ADGroupRoles:{Env}:ReadOnly").Value))
roles.Add(Roles.ReadOnly.ToString());
if(ADHelper.IsInGroup(AccountName, config.GetSection($"ADGroupRoles:{Env}:Admin").Value))
roles.Add(Roles.Admin.ToString());
return new GenericPrincipal(new GenericIdentity(AccountName), roles.ToArray());
}
我的角色枚举:
public enum Roles
{
[Description("ReadOnly")]
ReadOnly = 1,
[Description("Administrator")]
Admin = 4
}
好的,我能够找到答案,正如King所说,我只需要删除我的基本控制器中的匿名属性。虽然我的主要问题是,当我在Chrome中启动的IIS Express中运行我的web应用程序时,Windows身份验证不起作用。所以我需要切换到IE或MS Edge。幸运的是,MS Edge与Chrome非常相似,因此我没有丢失Chrome中所需的任何web开发工具
TLDR:Windows身份验证在Chrome中启动的IIS Express中不起作用,仅在IE和MS Edge中起作用。为什么需要在基本控制器上使用
AllowAnonymous
?这完全没有道理AllowAnonymousAttribute
仍然影响派生控制器,并且比所有其他AuthorizeAttribute
优先级更高(这意味着所有这些都将被忽略,而只影响AllowAnonymousAttribute
)。很简单,只需删除基本控制器上的allowanonymous属性
。如果您的代码不能修改,那么解决方案可能会更复杂。您好@KingKing,我已尝试删除基类控制器中的匿名属性,但遇到以下错误:System.InvalidOperationException:未指定authenticationScheme,也未找到DefaultChallengeScheme。所以我想我还需要授权他们的属性?
[Authorize(Roles = "ReadOnly")]
public class MatterController : AppSessionController
{
[Authorize(Roles = "ReadOnly")]
public ActionResult Details(int matterId)
{
return View();
}
}
public enum Roles
{
[Description("ReadOnly")]
ReadOnly = 1,
[Description("Administrator")]
Admin = 4
}