【发布时间】:2015-07-24 16:03:22
【问题描述】:
我有一个 Web Api 应用程序,它有以下问题。
[HttpGet]
[Route("Account/userName{userName}/password={password}/rememberMe/{rememberMe}")]
public HttpResponseMessage LogIn(string userName, string password, bool rememberMe)
{
if (User.Identity.IsAuthenticated)
{
return Request.CreateResponse(HttpStatusCode.Conflict, "already logged in.");
}
var dbPerson = dbContext.Persons.Where(x => x.UserName.Equals(userName) && x.EncryptedPassword.Equals(password)).FirstOrDefault();
if (dbPerson != null)
{
FormsAuthentication.SetAuthCookie(userName, rememberMe);
return Request.CreateResponse(HttpStatusCode.OK, "logged in successfully");
}
else
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
}
我从另一个 MVC 项目中调用。我得到了身份验证,但在下一页我正在调用 ajax 方法
var uri = 'http://localhost:44297/api/XXXX';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
for (var i = 0; i < data.$values.length; i++)
{
}
})
.fail(function() {
console.log( "error" )});
});
我收到 GET http://localhost:44297/api/StudyFocus 401(未经授权)。我该如何解决这个问题。我知道我需要通过这个 ajax 调用传递一些 cookie/会话值。但我不知道怎么做。任何人都可以用例子来解释我。
我的应用程序依赖于包括身份验证在内的 Web Api 项目。我需要使用表单身份验证使 web api 应用程序安全。任何帮助都是非常可观的。谢谢
【问题讨论】:
标签: asp.net-mvc authentication asp.net-web-api