加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用基于令牌的身份验证时,Web API 2 AccessFailedCo

发布时间:2020-12-16 07:25:22 所属栏目:asp.Net 来源:网络整理
导读:我使用带有Identity2.0 AccessFailedCount的Webapi,LockoutEndDateUtc没有在无效的用户名和密码上进行检测.我实现了WebAPI提供的基于令牌的身份验证.请帮忙 . 这是代码片段 using (UserManagerApplicationUser userManager = userManagerFactory) { Applicat
我使用带有Identity2.0 AccessFailedCount的Webapi,LockoutEndDateUtc没有在无效的用户名和密码上进行检测.我实现了WebAPI提供的基于令牌的身份验证.请帮忙 .

这是代码片段

using (UserManager<ApplicationUser> userManager = userManagerFactory)
        {
            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 (await userManager.IsLockedOutAsync(user.Id))
            {
                context.SetError("lock_out","The account is locked.");
                return;
            }

            if (!userManager.IsEmailConfirmed(user.Id))
            {
                context.SetError("inactive_user","The user is not active. Please check your Register Email to verify.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity,properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }

解决方法

最后我已经解决了这个代码

// To lock the user with userName ---- setting of maximum access 5 in IdentityConfig.cs File 
ApplicationUser userToLock = await userManager.FindByNameAsync(context.UserName);
if (userToLock != null)
{
    await userManager.AccessFailedAsync(userToLock.Id);
}

现在访问AccessFailedCount,LockoutEndDateUtc获取值

谢谢你的帮助.特别感谢@trailmax ……将我的想法转移到webapi

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读