【问题标题】:Web API 2 and ASP Identity - Handling of locked out usersWeb API 2 和 ASP 身份 - 锁定用户的处理
【发布时间】:2014-06-13 19:36:48
【问题描述】:

我刚刚将我的 Web 应用程序 (ASP.NET MVC) 迁移到 ASP Identity。

经过相当多的工作后一切正常,除了网络应用程序提供的 API。这是一个 WEB API 2,它使用不记名令牌机制来验证用户。身份验证本身也可以正常工作。但是:当用户被锁定时,该用户的令牌仍然通过 API-token-endpoint 颁发。

有没有建议的方法来处理这个问题?我没有找到任何例子......

谢谢!

【问题讨论】:

  • 不知道我是不是说的不清楚,还是真的很难回答。给出一个更一般的问题:是否可以用我自己的代码中断 Web API 2 的令牌创建过程,我可以在其中进行一些检查,并据此返回例如状态码或附加信息。

标签: asp.net-identity asp.net-web-api2


【解决方案1】:

好吧,那是一个愚蠢的...我现在看得更清楚了:)

我一直都在眼前:Web-Api2-Template 包含一个“ApplicationOAuthProvider”类。这个允许多个地方拦截管道...我选择了已经被覆盖的方法“GrantResourceOwnerCredentials”,然后在密码检查后直接检查用户是否被锁定。

抱歉,希望对大家有所帮助。

【讨论】:

    【解决方案2】:

    转到 ApplicationOAuthProvider 类并检查 ApplicationUser.LockoutEndDateUtc 中的 GrantResourceOwnerCredentials 方法:

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
                var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
    
                ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
    
                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
    
                if (user.LockoutEndDateUtc != null && user.LockoutEndDateUtc > DateTime.Now)
                {
                    context.SetError("User Locked", "User is Locked,please contact to system administrator");
                    return;
                }
                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager);
                ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager);
    
                AuthenticationProperties properties = CreateProperties(user.UserName);
                AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(cookiesIdentity);
            }
    

    【讨论】:

    • 此代码不是很好,因为“用户锁定”错误仅在用户和密码正确时才会显示。这意味着有人可以使用此方法来确定有效的用户名和密码。
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2018-06-30
    • 2016-04-05
    • 1970-01-01
    • 2014-01-28
    • 2017-07-27
    相关资源
    最近更新 更多