Esempio n. 1
0
        public void AuthenticateAndRetrieveUserTest()
        {
            var ctx     = GetContext();
            var userBus = new UserBusiness(ctx, WeblogConfiguration.Current);

            string email  = "*****@*****.**";
            User   result = userBus.AuthenticateAndRetrieveUser(email, "testing");

            Assert.IsNotNull(result, userBus.ErrorMessage);
            Assert.AreEqual(result.Username, email);
        }
Esempio n. 2
0
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            InitializeViewModel(model);

            if (!ModelState.IsValid)
            {
                model.ErrorDisplay.AddMessages(ModelState);
                model.ErrorDisplay.ShowError("", "Please correct the following");
                return(View(model));
            }

            var user = _userBus.AuthenticateAndRetrieveUser(model.Username, model.Password);

            if (user == null)
            {
                model.ErrorDisplay.ShowError(_userBus.ErrorMessage);
                return(View(model));
            }

            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            identity.AddClaim(new Claim("Fullname", user.Fullname));
            identity.AddClaim(new Claim("Username", user.Username));
            identity.AddClaim(new Claim("UserId", user.Id.ToString()));

            if (user.IsAdmin)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
            }

            // Set cookie and attach claims
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                          new ClaimsPrincipal(identity));


            if (!string.IsNullOrEmpty(model.RedirectUrl))
            {
                return(Redirect(model.RedirectUrl));
            }

            return(Redirect("~/"));
        }
        public override string Authenticate([FromBody] AuthenticateRequest auth)
        {
            var user = UserBusiness.AuthenticateAndRetrieveUser(auth.Username, auth.Password);

            if (user == null)
            {
                if (!string.IsNullOrEmpty(user.Username))
                {
                    var tok = UserTokens.FirstOrDefault(kv => kv.Value == user.Username);
                    UserTokens.TryRemove(tok.Key, out string t);
                }
                throw new UnauthorizedAccessException("Invalid Username or Password.");
            }

            var token = DataUtils.GenerateUniqueId(16);

            UserTokens[token] = user.Username;

            return(token);
        }
Esempio n. 4
0
        public async Task <ActionResult> SignIn(SigninViewModel model)
        {
            InitializeViewModel(model);

            if (!ModelState.IsValid)
            {
                model.ErrorDisplay.AddMessages(ModelState);
                model.ErrorDisplay.ShowError("Please correct the following:");
                return(View(model));
            }

            var user = _userBus.AuthenticateAndRetrieveUser(model.Email, model.Password);

            if (user == null)
            {
                model.ErrorDisplay.ShowError(_userBus.ErrorMessage);
                return(View(model));
            }

            var identity = AppUser.GetClaimsIdentityFromUser(user);


            // Set cookie and attach claims
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                          new ClaimsPrincipal(identity), new AuthenticationProperties
            {
                IsPersistent = true,
                AllowRefresh = true,
                ExpiresUtc   = DateTime.UtcNow.AddDays(2)
            });


            if (!string.IsNullOrEmpty(model.ReturnUrl))
            {
                return(Redirect(model.ReturnUrl));
            }

            return(Redirect("~/"));
        }