【问题标题】:WebAPI doesn't maintain FormsAuthentication over controllersWebAPI 不在控制器上维护 FormsAuthentication
【发布时间】:2015-06-25 01:08:37
【问题描述】:

情况是这样的:

我拥有一个 AccountController,它继承自 ApiController,看起来像这样

    [HttpPost]
    [ActionName("Login")]
    public HttpResponseMessage Login()
    {
        string emailOrUsername = base.Request.GetQueryNameValuePairs().ElementAt(0).Value;
        FormsAuthentication.SetAuthCookie(emailOrUsername, false);
        return Request.CreateResponse<string>(HttpStatusCode.OK, emailOrUsername);
    }
    [HttpGet]
    [Authorize]
    [ActionName("Logout")]
    public HttpResponseMessage Logout()
    {
        FormsAuthentication.SignOut();
        return Request.CreateResponse<string>(HttpStatusCode.OK, "Hope to see you again!");
    }

我还拥有一个 MessageController,它也继承自 ApiController,如下所示:

    [HttpPost]
    [Authorize]
    [ActionName("ReceiveMessage")]
    public HttpResponseMessage ReceiveMessage()
    {
        return Request.CreateResponse<string>(HttpStatusCode.OK, "Successfully received your message");
    }

所以,AccountController 完美运行,我登录并能够注销。 这是棘手和令人困惑的部分开始的地方。当我在同一台计算机 (localhost) 上进行测试时,一旦获得授权,我就可以访问 ReceiveMessage。 到目前为止一切顺利。

我现在已经编辑了 Documents\IISExpress\config\applicationhost.config 并编辑了绑定: 绑定协议="http" bindingInformation=":60967:"

所以我可以从其他设备访问我的 WebApi。我可以登录和注销,但我无法从这个新设备访问 ReceiveMessage(已登录) - 而在本地主机上,我可以做任何我想做的事情。

我做错了什么? (这是一个示例,我计划让多个用户连接到 api,所以在某处有一个静态 var 不是一个选项)

【问题讨论】:

  • 哪个设备?该设备是否支持 cookie?

标签: c# asp.net api controller


【解决方案1】:

Browser wise 工作得很好,@charlie Brown 是正确的,问题与 cookie 相关——我是从一个统一应用程序访问它

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2011-05-30
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多