azure - 如何在 Azure Functions 中使用 OWIN?

标签 azure owin azure-functions

我们有一个使用 WebAPI 和 OWIN 构建的 API。现在,这意味着许多身份验证代码使用 OWIN 类,其中大部分是 OwinContext,但是当转换为在 Azure Functions 中运行时,您基本上会沦为接收 HttpRequestMessage 的控制台应用程序。

为了不必重新实现我们的整个授权/身份验证方案,有没有办法在Azure Functions中使用OWIN?也许通过手动创建 OwinContext 或者其他什么?

如果这个问题太宽泛,我很抱歉,但我似乎无法理解这个问题。

最佳答案

Maybe by manually creating an OwinContext or something?

是的,我们可以根据请求消息手动创建。基于protocol of OWIN ,我们需要将所需的键添加到环境字典中。以下示例代码供您引用。

Dictionary<string, object> environment = new Dictionary<string, object>();

MemoryStream requestBody = new MemoryStream();
byte[] json = Encoding.UTF8.GetBytes("{ 'a' : 'value1' }");
requestBody.Write(json, 0, json.Length);
environment.Add("owin.RequestBody", requestBody);
var headers = new Dictionary<string, string[]>();
headers.Add("host",new string[] { "myhost.com" });
headers.Add("Content-Type", new string[] { "application/json" });
environment.Add("owin.RequestHeaders", headers);
environment.Add("owin.RequestMethod", "POST");
environment.Add("owin.RequestPath", "a.html");
environment.Add("owin.RequestPathBase", "/");
environment.Add("owin.RequestProtocol", "HTTP/1.1");
environment.Add("owin.RequestQueryString", "myhost.com");
environment.Add("owin.RequestScheme", "http");

OwinContext context = new OwinContext(environment);

context.Environment.Add("owin.ResponseHeaders", new Dictionary<string, string[]>());
context.Environment.Add("owin.ResponseBody", new MemoryStream());

关于azure - 如何在 Azure Functions 中使用 OWIN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46192435/

相关文章:

Azure 文件存储内容类型始终为 application/octet-stream

java - 在 Azure Web 应用上运行 Solr

firefox - Owin SSL firefox 无法验证接收数据的真实性错误

python - Azure Functions Python Blob 图像

azure - 应使用哪个指纹来列出使用 Windows Azure 管理 API 的托管服务

azure - 应用程序洞察部分丢失数据

asp.net-mvc-5 - VS 2017 asp.net MVC owin启动类在哪里

asp.net-web-api - 如何在 Swashbuckle 中包含类和属性描述,使用 OWIN 为 Web Api 2 生成 Swagger 文档?

azure - 无法从应用程序见解导出完整日志信息

Azure函数: Old code still running after a deployment