Web 中非常基本的不记名令牌身份验证和授权 API 2
Very basic bearer token authentication and authorization in Web API 2
我有一个内部网站,在我的组织内本地托管。同一站点还通过各种 Web 服务公开一些数据。它是使用 ASP.NET MVC 5 和 WebAPI 2 编写的,它是 .NET 4.5,而不是 Core。
目前用户可以使用 Windows 身份验证登录网站,一旦通过身份验证,他们就可以访问 API。但是,我还需要允许使用令牌访问 APIs,以便它们可以被自动化进程询问,因此我创建了一个页面,经过身份验证的用户可以在其中请求令牌。
我打算将此令牌用作不记名令牌,包含在对 Web API 的 header HTTP 请求中,以允许访问 APIs .据我了解,Bearer 令牌本质上代表了用户访问数据的权利,不需要任何其他信息(甚至是用户名)。
但是,我一直在努力寻找一个完整的 end-to-end 教程来验证和授权请求。这个站点上有一些问题和 Microsoft 文章,它们提供了一些很好的指示,但我觉得它们可能暗示了一些对我的要求来说过于复杂的东西。我不需要 return 任何类型的带有声明的身份或类似的东西,我根本不关心 OAuth。
我正在使用 Microsoft 的 Web API 框架,因此可以合理地假设从请求 header 中提取和检查令牌这样的基本操作应该相当简单!
有人可以概述我需要在我的应用程序中放置的组件和过程,以允许它从 HTTP 请求中提取 Bearer 令牌,使用我自己的代码检查其有效性,然后支持Authorize
Web 上的属性 API 令牌是否有效的方法?
看来我们有同样的需求,我也只需要一个快速的不记名令牌验证,不要让 API 完全敞开。
我从这里复制了大部分内容并对其进行了调整,因此它只检查 Bearer 令牌 https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
在 WebApiConfig.cs
中添加过滤器
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Add authentication
config.Filters.Add(new SimpleAuthenticationFilter()):
foo
}
}
SimpleAuthenticationFilter.cs
public class SimpleAuthenticationFilter : IAuthenticationFilter
{
private readonly string _bearerToken = ConfigurationManager.AppSettings["simpleToken"];
public bool AllowMultiple { get; }
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
// 1. Look for credentials in the request.
var request = context.Request;
var authorization = request.Headers.Authorization;
// 2. If there are no credentials, do nothing.
if (authorization == null)
{
context.ErrorResult = new AuthenticationFailureResult("Authorization header is 'null''", request);
return;
}
// 3. If there are credentials but the filter does not recognize the
// authentication scheme, do nothing.
if (!authorization.Scheme.Equals("Bearer"))
{
context.ErrorResult = new AuthenticationFailureResult("Authentication type must be 'Bearer'", request);
return;
}
// 4. If there are credentials that the filter understands, try to validate them.
// 5. If the credentials are bad, set the error result.
if (string.IsNullOrEmpty(authorization.Parameter))
{
context.ErrorResult = new AuthenticationFailureResult("Bearer token is null or empty", request);
return;
}
if (!authorization.Parameter.Equals(_bearerToken))
{
context.ErrorResult = new AuthenticationFailureResult("Bearer token invalid", request);
}
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}
}
AuthenticationFailureResponse.cs
public class AuthenticationFailureResult : IHttpActionResult
{
public AuthenticationFailureResult(string reasonPhrase, HttpRequestMessage request)
{
ReasonPhrase = reasonPhrase;
Request = request;
}
private string ReasonPhrase { get; }
private HttpRequestMessage Request { get; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
RequestMessage = Request, ReasonPhrase = ReasonPhrase
};
return response;
}
}
扩展上面 Min 的回答:
string token = Request.Headers.Authorization.ToString().Split(' ')[1];
我有一个内部网站,在我的组织内本地托管。同一站点还通过各种 Web 服务公开一些数据。它是使用 ASP.NET MVC 5 和 WebAPI 2 编写的,它是 .NET 4.5,而不是 Core。
目前用户可以使用 Windows 身份验证登录网站,一旦通过身份验证,他们就可以访问 API。但是,我还需要允许使用令牌访问 APIs,以便它们可以被自动化进程询问,因此我创建了一个页面,经过身份验证的用户可以在其中请求令牌。
我打算将此令牌用作不记名令牌,包含在对 Web API 的 header HTTP 请求中,以允许访问 APIs .据我了解,Bearer 令牌本质上代表了用户访问数据的权利,不需要任何其他信息(甚至是用户名)。
但是,我一直在努力寻找一个完整的 end-to-end 教程来验证和授权请求。这个站点上有一些问题和 Microsoft 文章,它们提供了一些很好的指示,但我觉得它们可能暗示了一些对我的要求来说过于复杂的东西。我不需要 return 任何类型的带有声明的身份或类似的东西,我根本不关心 OAuth。
我正在使用 Microsoft 的 Web API 框架,因此可以合理地假设从请求 header 中提取和检查令牌这样的基本操作应该相当简单!
有人可以概述我需要在我的应用程序中放置的组件和过程,以允许它从 HTTP 请求中提取 Bearer 令牌,使用我自己的代码检查其有效性,然后支持Authorize
Web 上的属性 API 令牌是否有效的方法?
看来我们有同样的需求,我也只需要一个快速的不记名令牌验证,不要让 API 完全敞开。
我从这里复制了大部分内容并对其进行了调整,因此它只检查 Bearer 令牌 https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
在 WebApiConfig.cs
中添加过滤器public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Add authentication
config.Filters.Add(new SimpleAuthenticationFilter()):
foo
}
}
SimpleAuthenticationFilter.cs
public class SimpleAuthenticationFilter : IAuthenticationFilter
{
private readonly string _bearerToken = ConfigurationManager.AppSettings["simpleToken"];
public bool AllowMultiple { get; }
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
// 1. Look for credentials in the request.
var request = context.Request;
var authorization = request.Headers.Authorization;
// 2. If there are no credentials, do nothing.
if (authorization == null)
{
context.ErrorResult = new AuthenticationFailureResult("Authorization header is 'null''", request);
return;
}
// 3. If there are credentials but the filter does not recognize the
// authentication scheme, do nothing.
if (!authorization.Scheme.Equals("Bearer"))
{
context.ErrorResult = new AuthenticationFailureResult("Authentication type must be 'Bearer'", request);
return;
}
// 4. If there are credentials that the filter understands, try to validate them.
// 5. If the credentials are bad, set the error result.
if (string.IsNullOrEmpty(authorization.Parameter))
{
context.ErrorResult = new AuthenticationFailureResult("Bearer token is null or empty", request);
return;
}
if (!authorization.Parameter.Equals(_bearerToken))
{
context.ErrorResult = new AuthenticationFailureResult("Bearer token invalid", request);
}
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
return Task.FromResult(0);
}
}
AuthenticationFailureResponse.cs
public class AuthenticationFailureResult : IHttpActionResult
{
public AuthenticationFailureResult(string reasonPhrase, HttpRequestMessage request)
{
ReasonPhrase = reasonPhrase;
Request = request;
}
private string ReasonPhrase { get; }
private HttpRequestMessage Request { get; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
RequestMessage = Request, ReasonPhrase = ReasonPhrase
};
return response;
}
}
扩展上面 Min 的回答:
string token = Request.Headers.Authorization.ToString().Split(' ')[1];