gpt4 book ai didi

asp.net - Web Api Asp.Net 身份

转载 作者:行者123 更新时间:2023-12-04 04:40:36 24 4
gpt4 key购买 nike

我只是想在 DelegatingHandler 中使用 Asp.Identity 对用户进行身份验证。

像上面这段代码:

public class TokenAuthentication : DelegatingHandler {
private readonly AuthenticationIdentityManager _identityManager;

public TokenAuthentication() {
_identityManager = new AuthenticationIdentityManager(new IdentityStore(new NFeDb()));
}

private Microsoft.Owin.Security.IAuthenticationManager AuthenticationManager {
get {
return HttpContext.Current.GetOwinContext().Authentication;
}
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
if (request.Headers.Contains("X-TokenCliente")) {
var tokenCliente = request.Headers.GetValues("X-TokenCliente").First();
var s = _identityManager.Authentication.SignIn(this.AuthenticationManager, tokenCliente, false);
if (s.Success) {
return await base.SendAsync(request, cancellationToken);
}
}

return request.CreateResponse(HttpStatusCode.Unauthorized);
}
}

但是,在我的 Controller 上使用 Authorize 符号:
[Authorize]
public HttpResponseMessage Get() {
return Request.CreateResponse(HttpStatusCode.OK);
}

我收到 302 状态 e 重定向到登录页面。是否可以在 DelegatingHandler 中进行身份验证?

更新:不知道是否需要使用OwinMiddleware

最佳答案

302 重定向可能来自 Cookie 中间件。

如果要使用 token 身份验证,最好使用 OWIN 不记名 token 中间件。

请查看:https://blogs.msdn.microsoft.com/webdev/2013/09/20/understanding-security-features-in-the-spa-template-for-vs2013-rc/

该博客介绍了如何在 Web api 中使用不记名 token 以及如何与 cookie 中间件并行工作。

关于asp.net - Web Api Asp.Net 身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962767/

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