c# - Web Api OWIN - 如何在每个请求上验证令牌
问题描述
我有两个应用程序
- 基于 ASP.NET MVC 构建的客户端应用程序
- 基于 Web API + OWIN 构建的身份验证服务器
已计划认证如下
- 对于用户登录客户端应用程序将使用登录凭据向身份验证服务器发出请求。
- 身份验证服务器将生成一个令牌并将其发送回客户端应用程序。
- 客户端应用程序将该令牌存储在本地存储中。
- 对于每个后续请求,客户端应用程序将附加令牌保存在请求标头中的本地存储中。
现在,在 CLEINT 应用程序的服务器端,我需要验证每个请求都附带的令牌没有被调和。
- 请建议我如何在每个请求中验证令牌,因为我不知道 OWIN 用于生成令牌的密钥。
- 编写代码来验证客户端应用程序上的令牌是正确的,或者它应该在身份验证服务器上。
- 我计划转移所有用户管理代码,例如注册用户,将密码更改为身份验证服务器,以便我们可以将其重新用于不同的客户端应用程序 - 这是正确的设计实践吗?
到目前为止,我已经编写了以下代码来创建 POC。
==========================OWIN配置========
[assembly: OwinStartup(typeof(WebApi.App_Start.Startup))]
namespace WebApi.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = false,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider(),
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new
OAuthBearerAuthenticationOptions());
}
}
}
==============================oAuth Provided========================
public class SimpleAuthorizationServerProvider: OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
}
请帮忙,
谢谢,
@保罗
解决方案
请建议我如何在每个请求中验证令牌,因为我不知道 OWIN 用于生成令牌的密钥。
您当前的设置(如果您已将 owin 管道添加app.UseOAuthBearerAuthentication()
到 owin 管道)将通过在每个请求中为您传递的不记名令牌对用户进行身份验证。然后可以通过 找到当前用户HttpContext.Current.User
。
然后使用该Authorize
属性决定哪些用户在某些端点上被授权。这是一个示例,其中允许具有“用户”角色的用户访问
[Authorize(Roles="user")]
public class ValuesController : ApiController
{
}
编写代码来验证客户端应用程序上的令牌是正确的,或者它应该在身份验证服务器上。
不,您不会在客户端验证令牌,如果您的用户凭据错误,您根本不会获得令牌。这就是你需要知道的。 而且,为什么要在客户端验证令牌?
我计划转移所有用户管理代码,例如注册用户,将密码更改为身份验证服务器,以便我们可以将其重新用于不同的客户端应用程序 - 这是正确的设计实践吗?
重用令牌提供者很常见。为什么要为每个应用程序发明轮子?构建一个伟大的,或使用第三方,并在您的应用程序中重用它。
推荐阅读
- firefox - 获取当前在 Youtube 中播放的视频
- python - 子类化 win32com 对象
- python - 为什么 tkinter 相对于窗口调整大小不成比例地扩展框架?
- bash - ROS 启动文件在 bash 文件中不起作用
- arrays - 在 Swift4 中检查填充的数组
- c++ - 将信息掩码为指针 - C++ (Boost.Intrusive)
- c++ - 我在使用带有类的数组时遇到什么问题?
- javascript - (Javascript)如何检查来自 navigator.geolocation.getCurrentPosition() 的长/纬度坐标是否在基于中心点的半径内?
- reactjs - 使用自行编写的基于 typescript 的 NPM 包时找不到模块
- python - OperatorNotAllowedInGraphError:在图形执行中不允许使用 `tf.Tensor` 作为 Python `bool`