【发布时间】:2013-10-21 13:45:34
【问题描述】:
我在我的 Web 应用程序中使用 WebAPI 服务。在此服务中用于所有帐户功能(/api/account/login、/api/account/logout、...)。在同一个 webroot 中,我有一个网站,它使用这个 webAPI 服务与后端系统进行通信。所以从我的 C# 代码中,我在我的 WebAPI 中调用 IsLoggedIn 函数,它在我登录时返回 true。这很好用。
[HttpGet]
public bool IsLoggedIn()
{
return (WebSecurity.IsAuthenticated);
}
在我的区域注册中,我添加了以下代码:
context.Routes.MapHttpRoute("Account", "api/account/{action}/{id}", new { Controller = "Account", id = RouteParameter.Optional });
以及以下 GlobalConfiguration:
GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlingAttribute());
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
自定义的 ExceptionHandlingAttribute 检查抛出的异常是否属于特定类型,并返回自定义的 ReasonPhrase,所以没什么特别的。
当我登录并从 javascript (jQuery) 或 Fiddler 中调用函数时,IsLoggedIn 函数返回 false。我应该在我的 jQuery 调用中添加什么以确保在 WebAPI 中仍然可以找到正确的用户?这发生在 POST 和 GET 调用中。
请帮忙:)
【问题讨论】:
标签: c# jquery asp.net-web-api