簡體   English   中英

遷移到 ASP.NET 3.1 - 從登錄控制器路由到 Razor 頁面主頁索引頁面不再有效

[英]Migration to ASP.NET 3.1 - routing from Login Controller to Razor Page Home Index page no longer working

我已將我的應用程序從 2.2 升級到 ASP.NET 核心 3.1。 但是,在成功登錄后,從我的 AuthenticationController Login 操作路由到 RazorPage 主頁區域索引頁面不再有效。 這是我的代碼的相關部分:

public void Configure(IApplicationBuilder app

    // Add JWToken to all incoming HTTP Request Header
    app.Use(async (context, next) =>
    {
        var JWToken = context.Session.GetString("JWToken");
        if (!string.IsNullOrEmpty(JWToken))
        {
            context.Request.Headers.Add("Authorization", "Bearer " + JWToken);
        }
        await next();
    });

    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
                                     name: "default",
                                     pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapRazorPages();
    });
}

// AuthenticationController
public class AuthenticationController : Controller { 
    ... 

    [HttpPost] 
    public async Task Login([Bind("Id, UserName, Password, EmailAddress")]UserLoginDetails login) {
        ... 
        // Successful user authentication - now time to route to Home Index Razor Page
        HttpContext.Session.SetString("JWToken", token); 
        return RedirectToPage("/Index", new { area = "HomePages" });

        ... 
    } 
}

namespace PilotApp.Areas.HomePages.Pages 
{ 
    [Authorize] 
    public class IndexModel : PageModel 
    { 
        ... 
    }
}     

RedirectToPage 函數導致 HTTP 401“此頁面無效”錯誤。 請注意,如果我刪除 [Authorize] 屬性,則此路由有效。 那么,為什么基於 JWT 的授權在 ASP.NET 3.1 中不起作用,而在 2.2 中起作用?

感謝任何幫助!

嘗試return RedirectToAction("Index", "HomePages");

ASP.NET 3.1 似乎需要以下 cookie 設置來確保 JWT 授權正常工作:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSession(options =>
        {       
            options.Cookie.IsEssential = true;
        });
        ...
    }

這解決了我的 ASP.NET 3.1 遷移問題:-)

暫無
暫無

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

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