我正在努力使用MVC ASP.NET核心应用程序登录,该应用程序也使用Web api(全部为asp.net核心)。
用户使用MVC应用程序登录-可以正常工作。
问题是,当他们调用网络API时,User
(如User.Identity
)始终为空
我用来验证的代码是
private static async Task Create(string value, string cookieName, Controller controller)
{
var claims = new List<Claim>
{
new Claim(cookieName, value),
new Claim(ClaimTypes.Role, "Administrator"),
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme
);
var authProperties = new AuthenticationProperties
{
AllowRefresh = true,
IsPersistent = true,
IssuedUtc = DateTime.UtcNow
};
await controller.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
}
然后我在Web API中调用端点时,User
始终为空。
如果我导航到另一个MVC控制器,则User
有一个值。
我猜这是因为我正在使用controller
登录(创建cookie),并且与web api
使用的控制器类型不同
我不确定该怎么做-我需要一些如何使cookie在MVC和WebApi端点上都持久存在吗?