How to set authentication cookie path

Andrus 121 Reputation points
2022-01-01T14:11:43.747+00:00

In ASP.NET 6 MVC Multi-tenant application tenants have different path base like /tenant1 and /tenant2.
Middlevare sets HttpContext PathBase from request url.
SignInAsync method always sets authentication cookie path to root, /

Trying to set authentication cookie path from PathBase using

Path = Request.PathBase.HasValue ? Request.PathBase.Value : "/"

in code below throws compile error since AuthenticationProperties does not have Path property.

How to set Cookie Path property so that different users can authenticated in different path bases ?

public class AccountController : Controller {
 public async Task<IActionResult> LogOn(string user, string password )  {

  // password verificaton removed 

  var claims = new List<Claim>  {
        new Claim(ClaimTypes.Name, user )
        };
  var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

  var authProperties = new AuthenticationProperties {
    IsPersistent = true,
    AllowRefresh = true,
// TODO: This throws compile error since Path property does not exist
   Path = Request.PathBase.HasValue ? Request.PathBase.Value : "/"

  };

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
   }
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,161 questions
{count} votes