ASP.NET Core Identity:如何重新定义禁止处理
ASP.NET Core Identity: How to redefine Forbid processing
情况:我从 PageModel return Forbid()
发起响应,重定向 (302) 到 /AccessDenied&returnUrl=...
这里我指定路径:
// configuration was used
serviceCollection.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/AccessDenied");
});
但是我怎样才能完全覆盖 "redirect response" 创建并指定我自己的 GET 参数(如 returnUrl
)或重新定义 returnUrl
值(我想传递我自己的值保存在自定义请求中 feature)?
或者 returning Forbid()
我可以手动重定向到 AccessDenied。但我想留在 Forbid()
的示范性和一致性。
更新:这也不起作用
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.ConfigureApplicationCookie(options =>
{
//options.AccessDeniedPath = new PathString("/AccessDenied");
options.Events.OnRedirectToAccessDenied = context =>
{
context.Response.Redirect("/AccessDenied&t=100");
return Task.CompletedTask;
};
});
// need for password reset functionality
serviceCollection.AddDbContext<WebUserDbContext>(optionsBuilder =>
optionsBuilder.UseSqlServer(ApplicationSettings.WebUserStorageConfiguration.ConnectionString));
serviceCollection.AddIdentity<WebUser, IdentityRole<int>>( options =>
{
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<WebUserDbContext>()
.AddDefaultTokenProviders();
// generally it is a version of routin by
serviceCollection.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
P.S。这是不需要身份验证但管理用户记录的服务站点。因此,站点未启用 MS Identity Lib 身份验证。身份类型刚刚被放入容器,因为我需要特定的身份 API 功能:"reset password"。这是愚蠢的情况:UserManager
相似的身份 API 类 是如此复杂,以至于除了从 DI/container 获得之外,不可能以其他方式构造它们。请不要在您的 API 中分享这种疯狂的 MS DI。
有趣的是,没有 serviceCollection.AddIdentity
浏览器在 .Forbid()
上收到 "Error 500" 并且断点仍然不会在 context.Response.Redirect("/AccessDenied&t=100");
上停止
重定向到已配置的 AccessDeniedPath
是 cookie 身份验证的默认行为。它会自动添加一个 returnUrl
参数,默认为当前 URL。如果您在身份验证属性中指定 RedirectUri
,则将使用 URL。
例如,控制器内部:
return Forbid(new AuthenticationProperties
{
RedirectUri = Url.Action("ActionAfterForbid"),
});
这基本上会生成指向 URL {AccessDeniedPath}?returnUrl=/exampleController/ActionAfterForbid
的重定向。
如果您需要更多自定义,您还可以覆盖 cookie 身份验证选项的 RedirectToAccessDenied
event:
services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToAccessDenied = context =>
{
// context.Options.AccessDeniedPath would be the configured path
// context.RedirectUri is the passed or automatically set up redirect URI
// context.HttpContext is the HttpContext if you want to modify features
// default behavior is basically this:
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
};
});
在那里,你可以做任何你想做的事情。
请注意,您必须在 调用 AddIdentity
之后调用 ConfigureApplicationCookie
。这是因为后者会为应用程序 cookie 本身配置一些默认值,因此如果您配置身份验证事件,它们将被 AddIdentity
覆盖。相反,你想用 ConfigureApplicationCookie
覆盖默认值,所以你必须在之后调用那个。
情况:我从 PageModel return Forbid()
发起响应,重定向 (302) 到 /AccessDenied&returnUrl=...
这里我指定路径:
// configuration was used
serviceCollection.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/AccessDenied");
});
但是我怎样才能完全覆盖 "redirect response" 创建并指定我自己的 GET 参数(如 returnUrl
)或重新定义 returnUrl
值(我想传递我自己的值保存在自定义请求中 feature)?
或者 returning Forbid()
我可以手动重定向到 AccessDenied。但我想留在 Forbid()
的示范性和一致性。
更新:这也不起作用
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.ConfigureApplicationCookie(options =>
{
//options.AccessDeniedPath = new PathString("/AccessDenied");
options.Events.OnRedirectToAccessDenied = context =>
{
context.Response.Redirect("/AccessDenied&t=100");
return Task.CompletedTask;
};
});
// need for password reset functionality
serviceCollection.AddDbContext<WebUserDbContext>(optionsBuilder =>
optionsBuilder.UseSqlServer(ApplicationSettings.WebUserStorageConfiguration.ConnectionString));
serviceCollection.AddIdentity<WebUser, IdentityRole<int>>( options =>
{
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<WebUserDbContext>()
.AddDefaultTokenProviders();
// generally it is a version of routin by
serviceCollection.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
P.S。这是不需要身份验证但管理用户记录的服务站点。因此,站点未启用 MS Identity Lib 身份验证。身份类型刚刚被放入容器,因为我需要特定的身份 API 功能:"reset password"。这是愚蠢的情况:UserManager
相似的身份 API 类 是如此复杂,以至于除了从 DI/container 获得之外,不可能以其他方式构造它们。请不要在您的 API 中分享这种疯狂的 MS DI。
有趣的是,没有 serviceCollection.AddIdentity
浏览器在 .Forbid()
上收到 "Error 500" 并且断点仍然不会在 context.Response.Redirect("/AccessDenied&t=100");
重定向到已配置的 AccessDeniedPath
是 cookie 身份验证的默认行为。它会自动添加一个 returnUrl
参数,默认为当前 URL。如果您在身份验证属性中指定 RedirectUri
,则将使用 URL。
例如,控制器内部:
return Forbid(new AuthenticationProperties
{
RedirectUri = Url.Action("ActionAfterForbid"),
});
这基本上会生成指向 URL {AccessDeniedPath}?returnUrl=/exampleController/ActionAfterForbid
的重定向。
如果您需要更多自定义,您还可以覆盖 cookie 身份验证选项的 RedirectToAccessDenied
event:
services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToAccessDenied = context =>
{
// context.Options.AccessDeniedPath would be the configured path
// context.RedirectUri is the passed or automatically set up redirect URI
// context.HttpContext is the HttpContext if you want to modify features
// default behavior is basically this:
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
};
});
在那里,你可以做任何你想做的事情。
请注意,您必须在 调用 AddIdentity
之后调用 ConfigureApplicationCookie
。这是因为后者会为应用程序 cookie 本身配置一些默认值,因此如果您配置身份验证事件,它们将被 AddIdentity
覆盖。相反,你想用 ConfigureApplicationCookie
覆盖默认值,所以你必须在之后调用那个。