作者:ReMadrism_FaithlU9D_1990 | 来源:互联网 | 2022-12-09 15:13
我有两个申请
客户端应用程序构建在ASP.NET MVC上
身份验证服务器构建在Web API + OWIN上
计划身份验证如下
对于用户登录,客户端应用程序将向具有登录凭据的身份验证服务器发出请求.
Authication服务器将生成令牌并将发送回客户端应用程序.
客户端应用程序将该令牌存储在本地存储中.
对于每个后续请求,客户端应用程序将附加的令牌保存在请求标头中的本地存
现在,在关闭应用程序的服务器端,我需要确认每个请求的令牌都没有被篡改.
请建议我如何在每个请求中验证令牌,因为我不知道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);
}
}
请帮忙,
谢谢,
@保罗