.NET Core WebAPI 永久令牌认证
.NET Core WebAPI permanent token authentication
我一直在看有关使用身份验证令牌保护 .NET Core WebAPI 的一个又一个教程,似乎所有内容都需要 username/password 组合才能获得临时令牌以用于针对 API 个控制器进行身份验证。
我正在处理的项目正在使用 Windows 物联网设备 运行 我编写的自定义 UWP 应用程序需要在后台连接到此 API 以便记录数据并下拉最新的设备配置。
我曾计划为每台设备提供一个唯一的令牌以进行身份验证,该令牌将在初始 device/app 设置期间输入和存储。我合作过的大多数第三方 APIs 只是发给你一个永久令牌,你可以用它来访问他们的 APIs。我想做类似的事情。
您可以使用标准的 JWT 方法,在 username/password 登录时创建两个令牌。
第一个令牌(访问令牌)是 short-lived,包含访问您的业务登录端点的权限。第二个(刷新令牌)是永久性的,允许您获取一个新的访问令牌,一旦它过期,创建一个连续的访问模式。刷新令牌应仅带有 refresh 声明,这将允许您访问专门用于创建新的短期令牌的端点。
那里有很多教程,例如 http://piotrgankiewicz.com/2017/12/07/jwt-refresh-tokens-and-net-core/
JWT 对于我的目的来说似乎矫枉过正且过于复杂,所以我最终按照本教程采用了中间件解决方案:
https://www.youtube.com/watch?v=n0llyujNGw8
我最终使用以下代码创建了一个中间件 class:
public class TokenValidationMiddleware
{
private readonly RequestDelegate _next;
public TokenValidationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
bool validToken = false;
//Require HTTPS
if (context.Request.IsHttps)
{
//Skip token authentication for test controller
if (context.Request.Path.StartsWithSegments("/api/values"))
{
validToken = true;
}
//Token header exists in the request
if (context.Request.Headers.ContainsKey("Token"))
{
//Check for a valid device by API token in my DB and set validToken to true if found
if (repository.FindDeviceByAPIKey())
{
validToken = true;
}
}
if (!validToken)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await context.Response.WriteAsync("Invalid Token");
}
else
{
await _next.Invoke(context);
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.HttpVersionNotSupported;
await context.Response.WriteAsync("HTTP not supported");
}
}
}
public static class TokenExtensions
{
public static IApplicationBuilder UseTokenAuth(this IApplicationBuilder builder)
{
return builder.UseMiddleware<TokenValidationMiddleware>();
}
}
然后我刚刚添加了app.UseTokenAuth();到我的初创公司 class
我一直在看有关使用身份验证令牌保护 .NET Core WebAPI 的一个又一个教程,似乎所有内容都需要 username/password 组合才能获得临时令牌以用于针对 API 个控制器进行身份验证。
我正在处理的项目正在使用 Windows 物联网设备 运行 我编写的自定义 UWP 应用程序需要在后台连接到此 API 以便记录数据并下拉最新的设备配置。
我曾计划为每台设备提供一个唯一的令牌以进行身份验证,该令牌将在初始 device/app 设置期间输入和存储。我合作过的大多数第三方 APIs 只是发给你一个永久令牌,你可以用它来访问他们的 APIs。我想做类似的事情。
您可以使用标准的 JWT 方法,在 username/password 登录时创建两个令牌。
第一个令牌(访问令牌)是 short-lived,包含访问您的业务登录端点的权限。第二个(刷新令牌)是永久性的,允许您获取一个新的访问令牌,一旦它过期,创建一个连续的访问模式。刷新令牌应仅带有 refresh 声明,这将允许您访问专门用于创建新的短期令牌的端点。
那里有很多教程,例如 http://piotrgankiewicz.com/2017/12/07/jwt-refresh-tokens-and-net-core/
JWT 对于我的目的来说似乎矫枉过正且过于复杂,所以我最终按照本教程采用了中间件解决方案: https://www.youtube.com/watch?v=n0llyujNGw8
我最终使用以下代码创建了一个中间件 class:
public class TokenValidationMiddleware
{
private readonly RequestDelegate _next;
public TokenValidationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
bool validToken = false;
//Require HTTPS
if (context.Request.IsHttps)
{
//Skip token authentication for test controller
if (context.Request.Path.StartsWithSegments("/api/values"))
{
validToken = true;
}
//Token header exists in the request
if (context.Request.Headers.ContainsKey("Token"))
{
//Check for a valid device by API token in my DB and set validToken to true if found
if (repository.FindDeviceByAPIKey())
{
validToken = true;
}
}
if (!validToken)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await context.Response.WriteAsync("Invalid Token");
}
else
{
await _next.Invoke(context);
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.HttpVersionNotSupported;
await context.Response.WriteAsync("HTTP not supported");
}
}
}
public static class TokenExtensions
{
public static IApplicationBuilder UseTokenAuth(this IApplicationBuilder builder)
{
return builder.UseMiddleware<TokenValidationMiddleware>();
}
}
然后我刚刚添加了app.UseTokenAuth();到我的初创公司 class