gpt4 book ai didi

c# - ASP.NET Identity Web Api 2 与 MVC 5 登录 Cookie

转载 作者:太空狗 更新时间:2023-10-29 21:57:35 27 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC 5 和 WebApi 2,如果成功则尝试使用重定向进行 Ajax 登录。

对于身份验证,我正在使用默认情况下用于新 ASP.NET MVC 项目的 ASP.NET Identity 库。

我使用 MVC 5 一切正常,一切都很好。但就我的生活而言,我无法让标准 MVC Controller 仅返回简单的 JSON。 (它包装了我想在父对象中返回的 JSON)是的,我可以在客户端解决这个问题,但对我来说这很老套。

我的另一个选择似乎更好,是使用 WebApi,它返回我期望的对象(只是我的 JSON 作为正文)。但我遇到的问题是 ASP.NET Identity SignInManager 不发送 .ASPNet.Identity cookie,除非我返回 ActionResult。

下面是我的 WebApi Controller ,它返回正确的预期最小 JSON 但不发送 Set-Cookie 命令,因此任何重定向都将用户视为未登录。

[Authorize]
public class AccountController : ApiController
{
public AccountController(IApplicationSignInManager signInManager)
{
SignInManager = signInManager;
}

public IApplicationSignInManager SignInManager {....}

//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[NgValidateAntiForgeryToken]
public async Task<HttpResponseMessage> Login(LoginViewModel model)
{
// Temporarily using Dynamic
dynamic res = new ExpandoObject();

// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var status = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

res.status = status.ToString();

return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(res))
};
}
}

返回以下 Json 但没有 cookie:

{"status":"Success"}

如果我将其更改为返回 ActionResult 而不是 HttpResponseMessage,则会发送 Set-Cookie 命令,但 Json 包含在额外的属性中。

    public async Task<ActionResult> Login(LoginViewModel model)
{
// Temporarily using Dynamic
dynamic res = new ExpandoObject();

// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var status = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

res.status = status.ToString();

return new JsonResult{Data = res};
}

它返回 cookie 但包装了 Json:

 {"contentEncoding":null,"contentType":null,"data":{"status":"Success"},"jsonRequestBehavior":1,"maxJsonLength":null,"recursionLimit":null}

现在我猜测他的情况正在发生,因为 SignInManager 可能正在将 cookie 分配给之前生成的 HttpContext.Current.Response 对象。当我返回 JsonResult 时,ASP.NET 将此结果附加到 HttpContext.Current.Response 并发送给客户端,因此具有 cookie。

但是当我返回 HttpResponseMessage 时,ASP.NET 返回新创建的 HttpResponse,它没有 SignInManager cookie。我这样想对吗?

编辑 1: 按照@David Tansey 的建议,我尝试了以下方法,它仍然没有设置 cookie,但返回正确的 Json

    public async Task<IHttpActionResult> Login(LoginViewModel model)
{
var status = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

return Json(new {status = status.ToString()});
}

返回正确的 json,但没有 cookie:

{"status":"Success"}

修复:在@David Tansey 指出使用匿名类型 new { } 后,我决定尝试一下。而下面两种方法就可以了

MVC

必须返回一个 ActionResult/JsonResult,其中除 Data 之外的所有字段均为空,并且必须返回一个 Anonymous 类型而不是一个动态的 ExpandoObject() 作为动态对象导致序列化程序膨胀返回的 Json

    [HttpPost]
[AllowAnonymous]
[NgValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
var status = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

//return Json(new {status = status.ToString()});
// OR
return new JsonResult { Data = new { status = status.ToString() } };
}

WebAPI 2

必须将返回类型更改为对象,然后将其序列化为 Json,并设置 cookie。返回 HttpResponseMessage 会导致 SignInManager 设置的 cookie 丢失,我猜是因为它开始使用新返回的响应对象。

    [HttpPost]
[AllowAnonymous]
[NgValidateAntiForgeryToken]
public async Task<object> Login(LoginViewModel model)
{
var status = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

return new {status = status.ToString()};
}

最佳答案

也许您已将 AuthenticationMode 设置为 Passive。尝试将其更改为事件。

来源 http://brockallen.com/2013/10/27/host-authentication-and-web-api-with-owin-and-active-vs-passive-authentication-middleware/

关于c# - ASP.NET Identity Web Api 2 与 MVC 5 登录 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com