Beispiel #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    ScarfAudit.UserCreated(model.UserName);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            ScarfAudit.Failed();
            return(View(model));
        }
        public void AuditFailureWithModelStateErrors_IsLogged()
        {
            using (IScarfContext context = ScarfLogging.BeginInlineContext())
            {
                var attribute = new LogAuditAttribute(MessageType.AuditLogin);
                BeforeAction(attribute);
                ScarfAudit.Failed();
                AfterAction(attribute, true);
            }

            Assert.AreEqual(1, dataSource.Messages.Count);
        }
Beispiel #3
0
        public async Task <ActionResult> Manage(ManageUserViewModel model)
        {
            bool hasPassword = HasPassword();

            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl        = Url.Action("Manage");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        ScarfAudit.PasswordChanged();
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

                    if (result.Succeeded)
                    {
                        ScarfAudit.PasswordChanged();
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            ScarfAudit.Failed();
            return(View(model));
        }
        public void AuditFailure_EnabledFailuresOnly()
        {
            auditElementMock.SetupGet(d => d.Enabled).Returns(true);
            auditElementMock.SetupGet(d => d.LogOnlyFailures).Returns(true);

            using (IScarfContext context = ScarfLogging.BeginInlineContext())
            {
                ScarfAudit.Start(MessageType.AuditResetPassword);
                ScarfAudit.Failed();
                context.Commit();
            }

            Assert.AreEqual(1, dataSource.Messages.Count);
            Assert.AreEqual(MessageClass.Audit, dataSource.Messages.First().MessageClass);
        }
 public override void OnActionExecuted(HttpActionExecutedContext filterContext)
 {
     if (ScarfAudit.HasResult == false)
     {
         if (filterContext.Exception != null)
         {
             ScarfAudit.Failed();
         }
         else
         {
             ScarfAudit.Succeeded();
         }
     }
     base.OnActionExecuted(filterContext);
 }
Beispiel #6
0
        public void LoginAuditFailed()
        {
            using (IScarfContext context = ScarfLogging.BeginInlineContext())
            {
                ScarfAudit.Start(MessageType.AuditLogin);
                ScarfAudit.Failed();
                context.Commit();
            }

            ScarfLogMessage msg = dataSource.Messages.Single();

            Assert.AreEqual(MessageClass.Audit, msg.MessageClass);
            Assert.AreEqual(MessageType.AuditLogin, msg.MessageType);
            Assert.AreEqual(false, msg.Success.Value);
        }
Beispiel #7
0
 public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
 {
     if (ScarfAudit.HasResult == false)
     {
         if (filterContext.Canceled ||
             (filterContext.Exception != null &&
              filterContext.ExceptionHandled == false))
         {
             ScarfAudit.Failed();
         }
         else
         {
             ScarfAudit.Succeeded();
         }
     }
     base.OnActionExecuted(filterContext);
 }
Beispiel #8
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);

                if (user != null)
                {
                    await SignInAsync(user, model.RememberMe);

                    ScarfAudit.LoggedInAs(user.UserName);
                    return(RedirectToLocal(returnUrl));
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            // If we got this far, something failed, redisplay form
            ScarfAudit.Failed();
            return(View(model));
        }