亲爱的
我已经按照https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint设计了一个OData端点 我已经完成了,但是有一个约束:对于所有API功能,标头都必须包含一个授权密钥。在服务文档中的所有地方都会完成此操作。服务文档是您在查看URL https://SITE/api/v1时得到的:
{"@odata.context":"http://SITE/api/v1/$metadata","value":[{"name":"things","kind":"EntitySet","url":"things"},{"name":"others","kind":"EntitySet","url":"others"}]}
因此,您知道如何生成此类文档服务吗?可以编辑吗?甚至,是否有办法检查此类文档服务中的标题? 预先感谢您的帮助,
亲切的问候,
答案 0 :(得分:0)
使用以下模块自行解决:
using System;
using System.Linq;
using System.Web;
namespace Api.Modules
{
public class AuthorizationModule : IHttpModule
{
public void Dispose(){}
private void Context_Authorization(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (!IsAuthorized(application.Context))
{
application.Context.Response.Status = "403 Forbidden";
application.Context.Response.StatusCode = 403;
application.CompleteRequest();
}
}
public bool IsAuthorized(HttpContext context)
{
if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization"))
return false;
string authHeader = HttpContext.Current.Request.Headers["Authorization"];
if (!authHeader.Equals(Properties.Resources.Authorization_Key))
{
return false;
}
return true;
}
public void Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(Context_Authorization);
}
}
}
如果有帮助...
亲切的问候,