public void ValidatePasswordTest7()
        {
            UserService target          = CreateNewUserService();
            UserChangePasswordAdminSP p = new UserChangePasswordAdminSP();

            p.UserName    = "******";
            p.NewPassword = "******";
            target.ChangePasswordAdmin(p); // make sure that password never changed
            try
            {
                UserValidateUserNamePasswordSP pv = new UserValidateUserNamePasswordSP();
                pv.UserName     = p.UserName;
                pv.Password     = p.NewPassword;
                pv.ThrowIfError = true;
                var actual = target.ValidateUserNamePassword(pv);
                Assert.AreNotEqual(null, actual); // should never happen
            }
            catch (UserException ex)
            {
                Assert.AreEqual(ex.Message, BusinessErrorStrings.PasswordValidation.AccountFailedPasswordLocked);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ValidatePasswordForFailedPasswordCount2()
        {
            UserService target          = CreateNewUserService();
            string      userName        = "******";
            string      password        = "******";
            string      invalidPassword = "******";

            // Step 1: Reset conditions (Count = LastPossibleAttempt)
            ResetFailedPasswordCount(userName, password, ConfigurationBase.DefaultInstance.FailedPasswordCountAllowed);
            // Step 2: Validate with invalid password to get invalid password message
            try
            {
                UserValidateUserNamePasswordSP pv = new UserValidateUserNamePasswordSP();
                pv.UserName     = userName;
                pv.Password     = invalidPassword;
                pv.ThrowIfError = true;
                var actual = target.ValidateUserNamePassword(pv);
                Assert.AreNotEqual(null, actual); // should never happen
            }
            catch (UserException ex)
            {
                Assert.AreEqual(ex.Message, BusinessErrorStrings.PasswordValidation.UserName_Or_Password_Is_Not_Correct);
            }
            catch (Exception)
            {
                throw;
            }

            // Step 3: Validate with valid password and check if you get locked exception
            try
            {
                UserValidateUserNamePasswordSP pv = new UserValidateUserNamePasswordSP();
                pv.UserName     = userName;
                pv.Password     = password;
                pv.ThrowIfError = true;
                object actual = target.ValidateUserNamePassword(pv);
            }
            catch (UserException ex)
            {
                Assert.AreEqual(ex.Message, BusinessErrorStrings.PasswordValidation.AccountFailedPasswordLocked);
            }
            catch (Exception)
            {
                throw;
            }
            // Step 4: Validate field values stored in database
            User user = (User)target.GetByUserNameT(userName);

            Assert.AreEqual(ConfigurationBase.DefaultInstance.FailedPasswordCountAllowed + 1, user.FailedPasswordAttemptCount);
            Assert.AreEqual((int)EntityEnums.UserApprovalStatusEnum.FailedPasswordLocked, user.UserApprovalStatusID);
        }
        public void ValidatePasswordTest()
        {
            UserService target = CreateNewUserService();
            UserValidateUserNamePasswordSP p = new UserValidateUserNamePasswordSP();

            p.UserName     = "******";
            p.Password     = "******";
            p.ThrowIfError = false;
            long expected = 1; //UserID
            User actual   = (User)target.ValidateUserNamePassword(p);

            Assert.AreEqual(expected, actual.UserID);
            Assert.IsTrue(actual.LastLoginDate.Value.AddMinutes(1) > DateTime.UtcNow); // recently changed :)
        }
        public void ValidatePasswordTest2()
        {
            UserService target = CreateNewUserService();
            UserValidateUserNamePasswordSP p = new UserValidateUserNamePasswordSP();

            p.UserName     = "******";
            p.Password     = "******";
            p.ThrowIfError = false;
            object expected = null;

            ResetFailedAttemp(p.UserName, 0); //reset situation
            object actual = target.ValidateUserNamePassword(p);

            Assert.AreEqual(expected, actual);
        }
        public void ValidatePasswordForFailedPasswordCount()
        {
            UserService target = CreateNewUserService();
            UserValidateUserNamePasswordSP p = new UserValidateUserNamePasswordSP();

            p.UserName     = "******";
            p.Password     = "******";
            p.ThrowIfError = false;
            object expected = null;

            ResetFailedAttemp(p.UserName, 1); //reset situation
            object actual = target.ValidateUserNamePassword(p);

            Assert.AreEqual(expected, actual);
            User user = target.GetByUserNameT(p.UserName);

            Assert.AreEqual(2, user.FailedPasswordAttemptCount);
        }
        public void ValidatePasswordTest4()
        {
            UserService target = CreateNewUserService();
            UserValidateUserNamePasswordSP p = new UserValidateUserNamePasswordSP();

            p.UserName     = "******";
            p.Password     = "******";
            p.ThrowIfError = true;
            try
            {
                var actual = target.ValidateUserNamePassword(p);
                Assert.AreNotEqual(null, actual); // should never happen
            }
            catch (UserException ex)
            {
                Assert.AreEqual(ex.Message, BusinessErrorStrings.PasswordValidation.UserName_Or_Password_Is_Not_Correct);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #7
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

                if (allowedOrigin == null)
                {
                    allowedOrigin = "*";
                }

                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

                User user = null;
                try
                {
                    if (context.UserName == "loginwithsinglesignontoken#")
                    {
                        var userId = UserEN.GetService("").LoginWithLoginToken(context.Password);
                        if (userId != null)
                        {
                            user = UserEN.GetService().GetByIDT(userId.Value, new GetByIDParameters());
                        }
                    }
                    else if (context.UserName == "loginwithregistertoken#")
                    {
                        user = UserEN.GetService("").LoginWithRegisterOffsiteInfo(context.Password);
                    }
                    else
                    {
                        UserValidateUserNamePasswordSP p = new UserValidateUserNamePasswordSP();
                        p.UserName     = context.UserName;
                        p.Password     = context.Password;
                        p.ThrowIfError = true;
                        user           = (User)UserEN.GetService("").ValidateUserNamePassword(p);
                    }
                }
                catch (UserException ex)
                {
                    context.SetError("invalid_grant", ex.Message);
                    return;
                }
                catch (Exception ex)
                {
                    context.SetError("invalid_grant", ex.Message);
                    return;
                }

                if (user != null)
                {
                    var    roleIds = UserInRoleEN.GetService("").GetRolesIDUserID(user.UserID.ToString());
                    string roleIdCommaSeparated = FWUtils.EntityUtils.ConvertObjectToString(roleIds);

                    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim(ClaimTypes.Name, user.UserID.ToString()));
                    identity.AddClaim(new Claim("sub", user.UserID.ToString()));
                    //identity.AddClaim(new Claim("role", "user"));
                    identity.AddClaim(new Claim("roleIds", roleIdCommaSeparated));
                    identity.AddClaim(new Claim("siteId", FWUtils.SecurityUtils.GetCurrentSiteID().ToString()));

                    var props = new AuthenticationProperties(new Dictionary <string, string>
                    {
                        {
                            "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                        },
                        {
                            "userName", user.UserID.ToString()
                        }
                    });

                    var ticket           = new AuthenticationTicket(identity, props);
                    var validationResult = context.Validated(ticket);
                    if (validationResult == false)
                    {
                        context.SetError("invalid_grant", "Ticket is not valid. Try again.");
                    }

                    // setting cookies for authentication in ASP.NET MVC
                    //    ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                    //CookieAuthenticationDefaults.AuthenticationType);

                    //CustomIdentity cidentity = new CustomIdentity(user.UserID.ToString(), user.Email.ToString());
                    ////ClaimsIdentity cookiesIdentity = new ClaimsIdentity(identity.Claims, CookieAuthenticationDefaults.AuthenticationType);
                    //ClaimsIdentity cookiesIdentity = new ClaimsIdentity(cidentity, identity.Claims, CookieAuthenticationDefaults.AuthenticationType, null, null);
                    //context.Request.Context.Authentication.SignIn(cookiesIdentity);

                    //FormsAuthentication.SetAuthCookie(user.UserID.ToString(), true);
                }
            }
            catch (Exception ex)
            {
                var msg = FWUtils.ExpLogUtils.ExceptionTranslator.TryToTranslate(ex).Message;
                context.SetError("error", msg);
            }
        }