我正在从OData控制器生成带有令牌的外部链接;如果我分别浏览此链接,但是每当在OData控制器内部调用未通过时,生成的链接就可以正常工作。 我感觉这可能是路由问题。 Route不知道如何处理此请求。
ODataController方法:
[HttpGet]
[ODataRoute("Token")]
//[Authorize] Do not know why it is alsways false
[AuthorizeWithAntiForgeryHeader]
public string Token()
{
return (Models.Token.NewToken().ToString());
}
内部授权属性未授权请求
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
if (this._AntiForgeryHeaderWasChecked)
{
http://127.0.0.1/login/AuthenticateUser?tok=abed-233de-frg23-45fd231;
//RedirectToLogin(actionContext);
//actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid request token");
}
else
{
base.HandleUnauthorizedRequest(actionContext);
}
}
在webAPIConfig中对我的路由配置进行采样
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*", exposedHeaders: "X-Antiforgery-Token,Authorization");
config.EnableCors(cors);
ODataModelBuilder builder = new ODataConventionModelBuilder();
FunctionConfiguration entFnToken = builder.Function("Token");
entFnToken.Returns<string>();
config.MapODataServiceRoute(routeName: "ODataRoute", routePrefix: "EntitleOData", model: builder.GetEdmModel());
}
它不会重定向到http://127.0.0.1/login/AuthenticateUser?tok=...
,但是,如果我分别浏览此URL,它将转到相应的ActionResult。