- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经为 Katana 实现了基本身份验证中间件(代码如下)。
(我的客户端托管在跨域上,然后才是实际的 API)。
The browser can skip the preflight request if the following conditionsare true:
The request method is GET, HEAD, or POST, and The application does notset any request headers other than Accept, Accept-Language,Content-Language, Content-Type, or Last-Event-ID, and The Content-Typeheader (if set) is one of the following:application/x-www-form-urlencoded multipart/form-data text/plain
在 javascript 中,我在服务器接受请求的所有请求上设置身份验证 header (使用 jquery,beforeSend)。这意味着上面将对所有请求发送选项请求。我不想要那个。
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
我该怎么做才能解决这个问题?我的想法是在他通过身份验证后将用户信息存储在 cookie 中。
我还在 katana 项目中看到 Microsoft.Owin.Security.Cookies - 这可能是我想要的,而不是我自己的基本身份验证吗?
BasicAuthenticationMiddleware.cs
using Microsoft.Owin;
using Microsoft.Owin.Logging;
using Microsoft.Owin.Security.Infrastructure;
using Owin;
namespace Composite.WindowsAzure.Management.Owin
{
public class BasicAuthenticationMiddleware : AuthenticationMiddleware<BasicAuthenticationOptions>
{
private readonly ILogger _logger;
public BasicAuthenticationMiddleware(
OwinMiddleware next,
IAppBuilder app,
BasicAuthenticationOptions options)
: base(next, options)
{
_logger = app.CreateLogger<BasicAuthenticationMiddleware>();
}
protected override AuthenticationHandler<BasicAuthenticationOptions> CreateHandler()
{
return new BasicAuthenticationHandler(_logger);
}
}
}
using Microsoft.Owin.Logging;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Infrastructure;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Composite.WindowsAzure.Management.Owin
{
public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
{
private readonly ILogger _logger;
public BasicAuthenticationHandler(ILogger logger)
{
_logger = logger;
}
protected override Task ApplyResponseChallengeAsync()
{
_logger.WriteVerbose("ApplyResponseChallenge");
if (Response.StatusCode != 401)
{
return Task.FromResult<object>(null);
}
AuthenticationResponseChallenge challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);
if (challenge != null)
{
Response.Headers.Set("WWW-Authenticate", "Basic");
}
return Task.FromResult<object>(null);
}
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
{
_logger.WriteVerbose("AuthenticateCore");
AuthenticationProperties properties = null;
var header = Request.Headers["Authorization"];
if (!String.IsNullOrWhiteSpace(header))
{
var authHeader = System.Net.Http.Headers.AuthenticationHeaderValue.Parse(header);
if ("Basic".Equals(authHeader.Scheme, StringComparison.OrdinalIgnoreCase))
{
string parameter = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter));
var parts = parameter.Split(':');
if (parts.Length != 2)
return null;
var identity = await Options.Provider.AuthenticateAsync(userName: parts[0], password: parts[1], cancellationToken: Request.CallCancelled);
return new AuthenticationTicket(identity, properties);
}
}
return null;
}
}
}
Options.Provider.AuthenticateAsync 验证用户名/密码并在通过身份验证时返回身份。
我要解决的问题是:我有一个使用 N Azure 云服务部署的 Owin 托管 WebAPI。它们中的每一个都链接到一个存储帐户,该存储帐户包含一个用户名/散列密码列表。
我正在从我的客户端向客户端添加这 N 个服务中的任何一个,然后可以通过它们的 webapis 与它们通信。他们被身份验证锁定。第一步是使用上面提供的列表通过基本身份验证方案验证用户。在那之后,我希望它可以很容易地添加其他身份验证方案,例如 Owin、UseWindowsAzureAuthentication 等或 UseFacebookAuthentication。 (我在这里确实遇到了挑战,因为除了添加服务的跨域站点之外,webapi 没有 Web 前端)。
如果你擅长 Katana 并且想和我一起做这方面的工作,请随时给我发邮件到 pks@s-innovations.net。我也会在最后在这里给出答案。
根据回答,我做了以下事情:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Application",
AuthenticationMode = AuthenticationMode.Active,
LoginPath = "/Login",
LogoutPath = "/Logout",
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = context =>
{
// context.RejectIdentity();
return Task.FromResult<object>(null);
},
OnResponseSignIn = context =>
{
}
}
});
app.SetDefaultSignInAsAuthenticationType("Application");
我假设它必须处于 AuthenticationMode = Active 状态,否则 Authorize 属性将不起作用?
在我的 webapi Controller 中究竟需要什么来交换 cookie?
public async Task<HttpResponseMessage> Get()
{
var context = Request.GetOwinContext();
//Validate Username and password
context.Authentication.SignIn(new AuthenticationProperties()
{
IsPersistent = true
},
new ClaimsIdentity(new[] { new Claim(ClaimsIdentity.DefaultNameClaimType, "MyUserName") }, "Application"));
return Request.CreateResponse(HttpStatusCode.OK);
}
以上可以吗?
我已经将我的 BasicAuthenticationMiddleware 添加为主动的,将上面的 CookieMiddleware 添加为被动的。
然后在 AuthenticateCoreAsync 中检查我是否可以使用 Cookie 登录,
var authContext = await Context.Authentication.AuthenticateAsync("Application");
if (authContext != null)
return new AuthenticationTicket(authContext.Identity, authContext.Properties);
所以我现在可以从 webapi Controller 交换用户名/传递给 cookie,我也可以直接使用基本方案进行不使用 cookie 的设置。
最佳答案
如果 web api 和 javascript 文件来自不同的来源,并且您必须在请求中添加授权 header 或 cookie header ,则无法阻止浏览器发送预检请求。否则会对任何 protected web api 造成 CSRF 攻击。
您可以使用 OWIN Cors package或 Web API Cors package启用 CORS 场景,它可以为您处理预检请求。
OWIN cookie中间件负责设置auth cookie并进行验证。这似乎是你想要的。
顺便说一句,基本身份验证挑战可能会导致浏览器弹出浏览器身份验证对话框,这在大多数 Web 应用程序中是不期望的。不确定这是不是你想要的。相反,使用 form post 发送用户名和密码并与 cookie 交换它们是常见的网络应用程序所做的。
如果您的计算机上安装了 VS 2013 RC 或 VWD 2013 RC,则可以创建一个启用了个人身份验证的 MVC 项目。该模板使用 cookie 中间件和表单登录后。虽然它是 MVC Controller ,但您可以简单地将代码转换为 Web API。
[更新]关于预检请求,根据规范,即使带有 cookie header 也会发送。您可以考虑添加 Max Age header 以使其缓存在浏览器中。 JSONP是另一种不需要预检的选项。
[Update2] 为了通过owin中间件设置cookie,请使用以下示例代码。
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.ApplicationAuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, "Test"));
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(identity, new AuthenticationProperties()
{
IsPersistent = true
});
关于c# - 使用基本身份验证将 cookie 添加到 Katana 托管的 WebAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18829777/
我想为我的 ABP 项目中的特定应用程序服务关闭自动 WebAPI 生成服务。 最佳答案 RemoteService 属性可用于将类标记为远程服务或禁用固有地实现 IRemoteService 接口(
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我有以下 Controller ,它应该接受用户名和密码作为 POST 中的有效负载。如果我将其更改为 HttpGet 就可以了。 [RoutePrefix("api")] public class
使用以下路线: routes.MapHttpRoute( name: "Set", routeTemplate: "api/set/{id}",
我正在使用 AngularJS,我正在尝试将 json 从我的服务发送到 webAPI Controller 。当我通过发送时,我在 webApi 函数的参数中收到 null。 我的功能服务是: an
据我了解,如果我有一个 ASP.NET WebApi 方法,其签名如下所示...... public HttpResponseMessage PostCustomer(Customer custome
我遇到了一个解决方案问题,我使用 Visual Studio SPA 模板中的部分在具有 Oauth 身份验证的 WebApi 中拥有帐户 Controller 。 app.UseOAuthBea
我按照此处的说明将 webApi.HelpPage 区域和 View 添加到使用 structureMap 的现有项目中 - 但是在访问/Help url 时: StructureMap Except
我有一个 WebAPI。如何返回并打开网页。例如,我想打开 CNN.com 页面。 [HttpGet] public HttpResponseMessage Get()
我想知道是否有人可以澄清这一点。我发现用法令人困惑。 链接和视频都没有回答我的问题 我知道像这样的链接 asp.net core middleware vs filters 甚至还有关于它的视频 但是
运行以下最新版本(在撰写本文时): Visual Studio 2019 16.4.5 .NET 核心 SDK 3.1.102 x64 测试的浏览器: 谷歌浏览器 80.0.3987.122 火狐 7
想法是,将有一个外部实体 (SharePoint) 调用我的 WebAPI 并传入 PDF 文件以及有关该 PDF 文件的一些额外元数据信息。我被困在如何构造 Web API 方法的签名上。这是我到目
我有一个 WebApi 服务处理来自简单表单的上传,如下所示: 但是,我不知道如何使用 HttpClient API 模拟同一
嗨,我是 Angular 的新手,现在我从一个示例登录页面开始,该页面传递包含用户名和密码的 userEntity。userEntity 是一个对象/实体是 webapi。 问题:当我为登录按钮单击
我有一个 AngularJS + MVC + WebAPI,我正在尝试:- 使用标准(个人账户)进行MVC认证;- 使用相同的用户和密码进行基于 WebAPI 的身份验证。 问题,AngularJS
Web API 的版本存在一些混淆。看看这个 Web API at NuGet , Microsoft ASP.NET Web API 2.2 5.2.3 什么?这里是没有提到 2.2 的描述 我的猜
我正在开发一个 Web 应用程序,该应用程序将 Owin 托管用于 MVC 和 WebApi 2。 我最近将 Microsoft Mvc/WebApi 包从 5.2.2 版升级到 5.2.3 版,将
随着Web技术的发展,现在各种框架,前端的,后端的,数不胜数。全栈工程师的压力越来越大。 现在的前端的框架,既可以做各种Web,又可以做各种APP,前端框架更新换代越来越快,越来越多。 传统的模
1. WebAPI 背景知识 1.1 什么是 WebAPI JS 分成三个大的部分: ECMAScript: 基础语法部分 DOM API: 操作页面结构 BOM API: 操作浏览器 WebAPI
我是一名优秀的程序员,十分优秀!