C# asp.NET核心-授权属性不强制重定向
我已经设置了一个新的asp.NET核心web项目。我想要自定义身份验证,在这里我创建自己的cookie并将声明分配给用户。这是相当直接的设置 我的Startup.cs代码如下所示:C# asp.NET核心-授权属性不强制重定向,c#,asp.net-mvc,redirect,asp.net-core,authorize,C#,Asp.net Mvc,Redirect,Asp.net Core,Authorize,我已经设置了一个新的asp.NET核心web项目。我想要自定义身份验证,在这里我创建自己的cookie并将声明分配给用户。这是相当直接的设置 我的Startup.cs代码如下所示: public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentR
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/account/login");
options.AccessDeniedPath = new PathString("/account/accessdenied");
options.AutomaticChallenge = true;
});
services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => {
policy.RequireClaim(ClaimTypes.Role, "admin"); });
});
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseStaticFiles();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
LoginPath = new PathString("/account/login"),
AccessDeniedPath = new PathString("/account/accessdenied"),
AutomaticChallenge = true
});
app.UseMvcWithDefaultRoute();
}
}
到目前为止一切都很好
有一件事我很难做到——当用户试图访问带有[Authorize]属性的视图时,他们不会被重定向到登录页面
我做错了什么
我还希望能够在稍后将用户重定向到拒绝访问页面,当他们与我打算定义的特定策略不匹配时
提前感谢您的指点 你在运行IIS Express吗?我在运行!当你这么说的时候,我试着通过NodeJs跑步,结果成功了!好的,那么这里有什么区别呢。最后,我的应用程序很可能会托管在windows和IIS上。我如何在该场景中也实现这一点?请参阅更新所有软件包(从1.0.1到1.1.0)首先产生此错误“项目未在“运行时”中列出“win10-x64、win81-x64、win7-x64”中的一个”。请参阅本文以进行修复:在此之后,项目编译良好,IIS Express按预期重定向。太棒了!有完全相反的问题:-/
[HttpPost]
public async Task<IActionResult> Login(string userName,
string password, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (!string.IsNullOrEmpty(userName) && userName == password)
{
List<Claim> claims;
switch (userName)
{
case "admin":
claims = new List<Claim>
{
new Claim("sub", "2"),
new Claim("name", "Bob"),
new Claim("email", "bob@smith.com"),
new Claim("status", "junior"),
new Claim("department", "sales"),
new Claim("region", "north"),
new Claim("role", "supervisor"),
new Claim(ClaimTypes.Role, "admin")
};
break;
default:
claims = new List<Claim>
{
new Claim("sub", "3"),
new Claim("name", userName),
new Claim("email", userName + "@smith.com"),
new Claim("status", "intern"),
new Claim("department", "development"),
new Claim(ClaimTypes.Role, "client")
};
break;
}
var id = new ClaimsIdentity(claims, "local");//, "local", "name", "role"
await HttpContext.Authentication.SignInAsync("Cookies",
new ClaimsPrincipal(id));
return LocalRedirect("/Home/Index");
}
return View();
}
[Authorize]
public IActionResult AccessibleToLoggedIn()
{
ViewData["Message"] = "Example - open to any logged in user!";
return View();
}
[Authorize(Policy ="AdminOnly")]
public IActionResult AdminPage()
{
ViewData["Message"] = "Admin only page";
return View();
}