public async Task <bool> Login([FromBody]  User loginUser)
        {
            var user = await accountRepo.AuthenticateAndLoadUser(loginUser.Username, loginUser.Password);

            if (user == null)
            {
                throw new ApiException("Invalid Login Credentials", 401);
            }


            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));

            if (user.Fullname == null)
            {
                user.Fullname = string.Empty;
            }
            identity.AddClaim(new Claim("FullName", user.Fullname));

            await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                         new ClaimsPrincipal(identity));

            return(true);
        }
Beispiel #2
0
        public async Task <object> Authenticate([FromBody] User loginUser)
        {
            var user = await accountRepo.AuthenticateAndLoadUser(loginUser.Username, loginUser.Password);

            if (user == null)
            {
                throw new ApiException("Invalid Login Credentials", 401);
            }

            // create a state object we can serialize as a single claim
            var UserState = new UserState();

            // track user state through our claim
            UserState.UserIdInt = user.Id;
            UserState.Name      = user.Fullname;
            UserState.Email     = user.Username;


            // create a new token with token helper and add our claim
            var token = JwtHelper.GetJwtToken(
                user.Username,
                Configuration.JwtToken.SigningKey,
                Configuration.JwtToken.Issuer,
                Configuration.JwtToken.Audience,
                TimeSpan.FromMinutes(Configuration.JwtToken.TokenTimeoutMinutes),
                new[]
            {
                new Claim("UserState", UserState.ToString())
            });

            return(new
            {
                token = new JwtSecurityTokenHandler().WriteToken(token),
                expires = token.ValidTo,
                displayName = user.Fullname
            });
        }
        public async Task<bool> Login([FromBody]  User loginUser)
        {
            var accountBus = new AccountRepository(context);
            var user = await accountBus.AuthenticateAndLoadUser(loginUser.Username, loginUser.Password);

            if (user == null)
                throw new ApiException("Invalid Login Credential", 401);


            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
            identity.AddClaim(new Claim(ClaimTypes.Name, user.Username))    ;
           
            if (user.Fullname == null)
                user.Fullname = string.Empty;
            identity.AddClaim(new Claim("FullName", user.Fullname));

            await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

            return true;
        }