当前位置: 首页>>代码示例>>C#>>正文


C# UserManager.ResetAccessFailedCount方法代码示例

本文整理汇总了C#中UserManager.ResetAccessFailedCount方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.ResetAccessFailedCount方法的具体用法?C# UserManager.ResetAccessFailedCount怎么用?C# UserManager.ResetAccessFailedCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UserManager的用法示例。


在下文中一共展示了UserManager.ResetAccessFailedCount方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidLogin

        bool ValidLogin(Login login)
        {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(userStore)
            {
                UserLockoutEnabledByDefault = true,
                DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                MaxFailedAccessAttemptsBeforeLockout = 3
            };
            var user = userManager.FindByName(login.UserName);

            if (user == null)
                return false;

            // User is locked out.
            if (userManager.SupportsUserLockout && userManager.IsLockedOut(user.Id))
            {
                return false;
            }

            // Validated user was locked out but now can be reset.
            if (userManager.CheckPassword(user, login.Password))
            {
                if (userManager.SupportsUserLockout
                 && userManager.GetAccessFailedCount(user.Id) > 0)
                {
                    userManager.ResetAccessFailedCount(user.Id);
                }
            }

            // Login is invalid so increment failed attempts.
            else {
                bool lockoutEnabled = userManager.GetLockoutEnabled(user.Id);
                if (userManager.SupportsUserLockout && userManager.GetLockoutEnabled(user.Id))
                {
                    userManager.AccessFailed(user.Id);
                    return false;
                }
                CaptchaHelper captchaHelper = new CaptchaHelper();
                string captchaResponse = captchaHelper.CheckRecaptcha();
                if (captchaResponse != "Valid")
                {
                    ViewBag.ErrorResponse = "The captcha must be valid";

                }
            }
            return true;
        }
开发者ID:andrewstec,项目名称:phase2_lastday,代码行数:48,代码来源:HomeController.cs

示例2: ValidLogin

        bool ValidLogin(Login login)
        {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(userStore)
            {
                UserLockoutEnabledByDefault = true,
                DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                MaxFailedAccessAttemptsBeforeLockout = 3
            };
            var user = userManager.FindByName(login.UserName);

            if (user == null)
                return false;

            // User is locked out. 
            if (userManager.SupportsUserLockout && userManager.IsLockedOut(user.Id))
                return false;

            // Validated user was locked out but now can be reset. 
            if (userManager.CheckPassword(user, login.Password)
                    && userManager.IsEmailConfirmed(user.Id))
            {
                if (userManager.SupportsUserLockout
                 && userManager.GetAccessFailedCount(user.Id) > 0)
                {
                    userManager.ResetAccessFailedCount(user.Id);
                }
            }
            // Login is invalid so increment failed attempts. 
            else {
                bool lockoutEnabled = userManager.GetLockoutEnabled(user.Id);
                if (userManager.SupportsUserLockout && userManager.GetLockoutEnabled(user.Id))
                {
                    userManager.AccessFailed(user.Id);
                    return false;
                }
            }
            return true;
        }
开发者ID:JayOwl,项目名称:Bruteforce-Attack-Prevention,代码行数:39,代码来源:HomeController.cs

示例3: BtnIniciar_Click1

        protected void BtnIniciar_Click1(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            //var user = userManager.Find(UserName.Text, Password.Text);

            var user = userManager.FindByName(UserName.Text);

            if (user != null)
            {
                var validCredentials = userManager.Find(UserName.Text, Password.Text);

                if (userManager.IsLockedOut(user.Id))
                {
                    ModelState.AddModelError("", string.Format("Your account has been locked out for {0} minutes due to multiple failed login attempts.", ConfigurationManager.AppSettings["DefaultAccountLockoutTimeSpan"].ToString()));
                    StatusText.Text = string.Format("Your account has been locked out for {0} minutes due to multiple failed login attempts.", ConfigurationManager.AppSettings["DefaultAccountLockoutTimeSpan"].ToString());
                }
                else if (userManager.GetLockoutEnabled(user.Id) && validCredentials == null)
                {
                    userManager.AccessFailed(user.Id);
                    string message;
                    if (userManager.IsLockedOut(user.Id))
                    {
                        message = string.Format("Your account has been locked out for {0} minutes due to multiple failed login attempts.", ConfigurationManager.AppSettings["DefaultAccountLockoutTimeSpan"].ToString());
                        StatusText.Text = message;
                    }
                    else
                    {
                        int accessFailedCount = userManager.GetAccessFailedCount(user.Id);
                        int attemptsLeft =
                            Convert.ToInt32(
                                ConfigurationManager.AppSettings["MaxFailedAccessAttemptsBeforeLockout"].ToString()) -
                            accessFailedCount;
                        message = string.Format(
                            "Invalid credentials. You have {0} more attempt(s) before your account gets locked out.", attemptsLeft);
                        StatusText.Text = message;
                    }

                    ModelState.AddModelError("", message);
                }
                else if (validCredentials == null)
                {
                    ModelState.AddModelError("", "Invalid credentials. Please try again.");
                    StatusText.Text = "Invalid credentials. Please try again.";
                }
                else
                {

                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    userManager.ResetAccessFailedCount(user.Id);

                    string vig = DateTime.Now.Year.ToString();
                    SetCookieUser(UserName.Text, vig);

                    string url = Request.QueryString["ReturnUrl"];
                    if (url == "" || url == null)
                    {
                        gesMenuAdapter mg = new gesMenuAdapter();
                        List<dataTree> l = mg.getOpciones("INICI", UserName.Text);
                        if (l.Where(t => t.roles == "INICIAdministrativo").FirstOrDefault() != null)
                        {
                            SetCookieRol("administrador");
                            url = "/Inicio/Administrativo/Inicio.aspx";
                        }
                        else
                        {
                            if (l.Where(t => t.roles == "INICIAcudientes").FirstOrDefault() != null)
                            {
                                SetCookieRol("acudiente");
                                url = "/Inicio/Acudientes/Inicio.aspx";
                            }
                        }
                    }
                    IdentityHelper.RedirectToReturnUrl(url, Response);
                }

            }
            else
            {
                StatusText.Text = "Invalid username or password.";
            }
        }
开发者ID:borisgr04,项目名称:ByA_InstEduc,代码行数:85,代码来源:login.aspx.cs


注:本文中的UserManager.ResetAccessFailedCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。