簡體   English   中英

如果用戶未登錄.NET Core,如何重定向用戶?

[英]How to redirect user if he is not logged in .NET Core?

我正在使用.NET Core中的Razor Pages開發Web應用程序。

如果用戶未登錄到登錄頁面,我想將其重定向。 如果他們登錄-系統應將用戶重定向到它的第一個請求的鏈接/原始鏈接。

這是我當前的Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
{

    ...

     app.Use(async (ctx, next) =>
     {
        await next();
        if(ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted)
          {
              //Re-execute the request so the user gets the error page
              string originalPath = ctx.Request.Path.Value;
              ctx.Items["originalPath"] = originalPath;
              ctx.Request.Path = "/error";
              await next();
          }
     });
     app.UseStaticFiles();

     app.UseAuthentication();

     app.UseMvc();

}

因此,例如,如果用戶請求/ Dashboard / Settings / Account / Password並且他沒有登錄,我想先將他重定向到Login頁面,在他提交后,他將被重定向到該鏈接,而不是/ Dashboard 。

我不確定如何適應此腳本(

編輯

// In my login
if (result.Succeeded)
{
 _logger.LogInformation("User logged in.");

  if(!string.IsNullOrEmpty(returnUrl))
    return Redirect(returnUrl);

  return RedirectToPage("/Dashboard/Index");
}

我認為問題就在這里。 我的ReturnUrl為null 因此,它總是重定向到主頁。

我為Cookie驗證所做的工作

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options => {
            options.LoginPath = "/Home/Login/"; // auth redirect
            options.ExpireTimeSpan = new TimeSpan(1,0,0,0 ); 
        });

    [Authorize] // set authorize will auto redirect to specified LoginPath
    public IActionResult yourmethod()

暫無
暫無

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

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