使用Breeze使用Web API进行表单身份验证

时间:2013-01-18 16:58:05

标签: breeze

我使用表单身份验证保护WebAPI,即使用Breezecontroller 当我尝试调用WebAPi方法时,我收到了以下错误。

状态:404 statusText:“未找到” 消息:“元数据查询失败:'';,找不到Http资源匹配...

我的问题是为什么我没有回复“UnAuthorized error(401)”? 元数据也用[授权]修饰。

似乎FormsAuthentication的重定向给出了问题。 它重定向到Login(具有AllowAnonymous)WebApi方法并报告它找不到,尽管我有。此外,我将Authrozie应用于方法而不是控制器。确切的错误是

  {"$id":"1","$type":"System.Web.Http.HttpError,System.Web.Http","Message":"NoHTTPresourcewasfoundthatmatchestherequestURI'http://localhost:40678/api/Country/Login?ReturnUrl=/api/Country/Metadata'.","MessageDetail":"Noactionwasfoundonthecontroller'Country'thatmatchestherequest."}

2 个答案:

答案 0 :(得分:3)

试过并且工作正常。我打赌你的网址有误。

这是我的控制器的预备:

[Authorize]
[BreezeController]
public class BreezeTodoController : ApiController
{
    private readonly BreezeTodoContext _context;

    public BreezeTodoController() {
        _context = new BreezeTodoContext(User);
    }

    [HttpGet]
    public string Metadata() {
        return _context.Metadata();
    }
    // ... more

我点击了这个网址

http://localhost:32377/api/breezetodox/metadata

我回到了401

Request URL:http://localhost:32377/api/breezetodo/metadata
Request Method:GET
Status Code:401 Unauthorized

但如果我在网址中犯了错误(请参阅breezetodo之后的'x')

Request URL:http://localhost:32377/api/breezetodox/metadata
Request Method:GET
Status Code:404 Not Found

如果我的动作名称不匹配(请参阅元数据后的'x'):

Request URL:http://localhost:32377/api/breezetodo/metadatax
Request Method:GET
Status Code:404 Not Found

换句话说,如果资源无法首先找到该资源,则无法报告该资源是未经授权的。

答案 1 :(得分:0)

使用[Authorize]标记BreezeController,然后尝试使用此链接直接检索Breeze Metadata:

Request URL:http://localhost/breeze/breeze/metadata

重定向到:

http://localhost/Login?ReturnUrl=%2Fbreeze%2Fbreeze%2Fmetadata with a 404

没有[授权],使用相同链接访问Breeze元数据工作正常。