代码之家  ›  专栏  ›  技术社区  ›  RezaNoei

如何在ASP中限制用户只能访问一个令牌。网络标识

  •  0
  • RezaNoei  · 技术社区  · 7 年前

    我在webApi应用程序中使用基于令牌的身份验证。对于每个登录,OAuth为用户生成一个访问令牌。如果用户尝试多次登录。它可能拥有一些更有效的令牌。 此过程是否有限制。

    这是我的创业课程:

     public void Configuration(IAppBuilder app)
     {
         HttpConfiguration config = new HttpConfiguration();
    
         ConfigureOAuth(app);
    
         WebApiConfig.Register(config);
         app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
         app.UseWebApi(config);
         //Rest of code is here;
     }
    
     public void ConfigureOAuth(IAppBuilder app)
     {
         OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
         {
             AllowInsecureHttp = true,
             TokenEndpointPath = new PathString("/token"),
             AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
             Provider = new SimpleAuthorizationServerProvider()
         };
    
         // Token Generation
         app.UseOAuthAuthorizationServer(OAuthServerOptions);
         app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
     }
    

    下面是“GrantResourceOwnerCredentials”方法:

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
     {
     context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
    
     using (AuthRepository _repo = new AuthRepository())
     {
         IdentityUser user = await _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);
    
     }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Sunil Shrestha    7 年前

    oauth令牌的主要限制之一是它的到期时间。所以,如果您生成长寿令牌,那么它将在很长时间内有效。因此,处理此类问题的一些常见方法是:

    • 使用其他刷新令牌颁发短期生存令牌

    • 将令牌存储在数据库中,每次生成新令牌时,将旧令牌状态设为过期。然后,您可以编写自定义的authorize属性来检查令牌是否过期。

        2
  •  0
  •   Victor Hugo Terceros    7 年前

    我担心令牌在到期之前是有效的,它将包含与用户相关的所有信息。

    因此,要做您想做的事情,您必须创建自己的层来验证用户是否有令牌,例如创建映射表,然后创建自定义过滤器,如果用户没有使用为他生成的最后一个令牌,则拒绝该请求。