簡體   English   中英

ASP.NET 核心 - 授權屬性登錄重定向后保留 POST 數據

[英]ASP.NET Core - Preserve POST data after Authorize attribute login redirect

這與這個問題本質上是相同的問題: ASP.NET MVC - Preserve POST data after Authorize attribute login redirect except 7 年前沒有問過,它是關於 ASP.NET Core,這是完全不同的。 我正在使用[Authorize]屬性進行最基本的訪問身份驗證,實際上只是為了檢查是否有用戶登錄。 如果沒有,那么它會將他們踢回登錄頁面。 這是為此設置的服務。

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.Cookie.Name = "loginId";
    });
services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/Logout");

這是我的注銷操作。


[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Logout(string returnUrl = "/cgi-s/admin.cgi")
{
    await HttpContext.SignOutAsync();
    if (HttpContext.Request.Cookies.TryGetValue("loginId", out string loginId))
    {
        User auth = _context.Users.Where(a => a.LoginId.Equals(loginId)).FirstOrDefault();
        if (auth != null)
        {
            auth.LoginId = string.Empty;
            await _context.SaveChangesAsync();
        }
    }
    return Redirect("/Account/Login?returnUrl="+returnUrl);
}

現在,我只是使用帶有返回 url 字符串的默認行為,以便在成功登錄后返回嘗試的頁面。 有沒有辦法設置它,以便也保留 POST 數據?

我嘗試了一些不同的東西,比如存儲發布數據的自定義中間件,然后在登錄時檢索這些數據,但我沒有想出任何我之后沒有發現安全漏洞的東西。

有沒有一種我只是想念的簡單方法來做到這一點?

謝謝。

PS。 請忽略注銷操作中發生的怪事。 We are a two man team working on a 20 year old Perl CGI site, slowly transitioning over to .NET while trying to also keep up with new features and bug fixes, so everything is weird while we run Perl CGI alongside some .NET code on IIS with郵遞員。 希望我們最終能把所有東西都過渡過來。

您是否嘗試過 httpContext.Request.EnableBuffering?

在這里看到這個問題,一定要看看評論: Read the body of a request as string inside middleware

 httpContext.Request.EnableBuffering();
 using(StreamReader streamReader = new StreamReader(httpContext.Request.Body, 
 ...,leaveOpen: true))
{

  //Do something here:


   httpContext.Request.Body.Position = 0;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM