此页面无法正常工作本地主机重定向太多次MVC

问题描述 投票:0回答:1

Too many redirects

Error with many encoded ReturnUrl parameters

带有Cookie认证的Program.cs文件:

builder.Services.AddAuthentication("CookieAuthentication").AddCookie("CookieAuthentication", options =>
            {
                options.LoginPath = "/<Login/LoginView";
                options.AccessDeniedPath = "/Login/AccessDenied";
            });

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

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

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Login}/{action=Signup}/{id?}");

app.Run();

登录控制器:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult LoginView(string username, string password)
        {
            if (!ModelState.IsValid)
            {
                //Error code here
            }
            
            if (!UserExists(username, password))//Check if user exists in Database
            {
                //Error code here
            }
           
            TempData["Username"] = username;
            return RedirectToAction("Index", "Home");
            //I used breakpoint here and this code runs but doesn't work properly.
        }

我还在 Home Controller 上使用了 [Authorize] 属性来防止用户在未登录的情况下访问它。Login/LoginView 是登录愤怒路径。

asp.net-mvc asp.net-core
1个回答
1
投票

此页面无法正常工作本地主机重定向太多次 MVC

对于您当前的场景,请务必在

[AllowAnonymous]
中的
Index
操作上添加
HomeController
。而你的
LoginPath
/Home/Index
,不需要授权。

[Authorize]
public class HomeController : Controller
{
    [AllowAnonymous]
    public async Task<IActionResult> Index()
    {
        //do your stuff...
        return View();            
    }
    //....
}

更新:

Cookie认证登录

程序.cs:

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
    options.LoginPath = "/Login/LoginView";
    options.AccessDeniedPath = "/Login/AccessDenied";
});
        
var app = builder. Build();
       
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseAuthentication();   //be sure add authentication and authorization middleware.....
app.UseAuthorization();

//...

如何让用户感叹:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LoginView(string username, string password)
{
     if (!ModelState.IsValid)
     {
        //Error code here
     }
        
     if (!UserExists(username, password))//Check if user exists in Database
     {
        //Error code here
     }                      
    var claims = new List<Claim>
    {
        new Claim(ClaimTypes.NameIdentifier,username)   //add the claims you want...
    };
    //authProperties you can choose any option you want, below is a sample...
    var authProperties = new AuthenticationProperties
    {
        //IssuedUtc = DateTimeOffset.UtcNow,
        //ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1),
        //IsPersistent = false
    };
    var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

    TempData["Username"] = username;
    return RedirectToAction("Index", "Home");
    
}
© www.soinside.com 2019 - 2024. All rights reserved.