gpt4 book ai didi

c# - 使用基本身份验证将 cookie 添加到 Katana 托管的 WebAPI

转载 作者:太空狗 更新时间:2023-10-29 23:39:24 30 4
gpt4 key购买 nike

我已经为 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);
}
}
}

BasicAuthenticationHandler.cs

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 packageWeb 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com