Joh*_*ver 4 cookies asp.net-core-2.0
如何在Cookie选项中将中间件应用于OnRedirectToLogin事件(以便可以使用依赖项注入),或者如何从RedirectContext检索actionContext?我曾尝试搜索文档或示例,但是很难找到,也没有看到任何示例说明或定义方法。这有可能吗?
我的Startup.cs
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
o.AccessDeniedPath = new PathString("/Error/AccessDenied");
o.LoginPath = new PathString("/Account/Login/");
o.Cookie.Path = "/";
o.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
o.Cookie.HttpOnly = true;
o.LogoutPath = new PathString("/Account/Logout/");
o.Events.OnRedirectToLogin = (context) =>
{
var routeData = context.HttpContext.GetRouteData();
RouteValueDictionary routeValues = new RouteValueDictionary();
if (routeData != null) routeValues.Add("lang", routeData.Values["lang"]);
Uri uri = new Uri(context.RedirectUri);
string returnUrl = HttpUtility.ParseQueryString(uri.Query)[context.Options.ReturnUrlParameter];
string focustab = "";
context.Request.Query.ToList().ForEach(x =>
{
if (x.Key == "id") routeValues.Add("id", x.Value.FirstOrDefault());
if (x.Key == "values") routeValues.Add("values", x.Value.FirstOrDefault());
});
routeValues.Add(context.Options.ReturnUrlParameter, returnUrl + focustab);
//context here is a redirect context, how can i get the action context to create a new Action as what UrlHelper is expecting
context.RedirectUri = new UrlHelper(context).Action("login", "account", routeValues);
return Task.CompletedTask;
};
});
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我能够按需要运行它,因此我发布了答案,以防其他人遇到此问题。在本文档的帮助下:使用没有ASP.NET Core Identity的cookie身份验证。我实现了CustomCookieAuthenticationEvents并将其强制转换为Cookie事件的类型。
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
o.AccessDeniedPath = new PathString("/Error/AccessDenied");
o.LoginPath = new PathString("/Account/Login/");
o.Cookie.Path = "/";
o.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
o.Cookie.HttpOnly = true;
o.LogoutPath = new PathString("/Account/Logout/");
//This line here
o.EventsType = typeof(CustomCookieAuthenticationEvents);
});
//register the IActionContextAccessor so that I can inject into my CustomCookieAuthenticationEvents constructor
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
Run Code Online (Sandbox Code Playgroud)
然后,在该Custom类中,我能够进行注入IUrlHelperFactory,IActionContextAccessor这有助于我UrlHelper为当前操作创建新内容。
public class CustomCookieAuthenticationEvents : CookieAuthenticationEvents
{
private IUrlHelperFactory _helper;
private IActionContextAccessor _accessor;
public CustomCookieAuthenticationEvents(IUrlHelperFactory helper, IActionContextAccessor accessor)
{
_helper = helper;
_accessor = accessor;
}
public override Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context)
{
var routeData = context.Request.HttpContext.GetRouteData();
RouteValueDictionary routeValues = new RouteValueDictionary();
//Create new routeValues based on routeData above
... code removed for brevity
Uri uri = new Uri(context.RedirectUri);
string returnUrl = HttpUtility.ParseQueryString(uri.Query)[context.Options.ReturnUrlParameter];
routeValues.Add(context.Options.ReturnUrlParameter, returnUrl + focustab);
var urlHelper = _helper.GetUrlHelper(_accessor.ActionContext);
context.RedirectUri = UrlHelperExtensions.Action(urlHelper, "login", "account", routeValues);
return base.RedirectToLogin(context);
}
}
Run Code Online (Sandbox Code Playgroud)
这使我可以修改“重定向到登录”请求上的查询字符串和returnUrl参数。我的要求是在首次登录时删除特定的查询字符串值,这可以满足我的需求。
| 归档时间: |
|
| 查看次数: |
2875 次 |
| 最近记录: |