Exemplo n.º 1
0
        public async Task <dynamic> dangNhap(ThongTinDangNhap thongtinDN)
        {
            NguoiDung ngDung;

            using (var connection = new SqlConnection(connectionstr))
            {
                var p = new DynamicParameters();
                p.Add("@TaiKhoan", thongtinDN.TaiKhoan);
                p.Add("@MatKhau", thongtinDN.MatKhau);
                ngDung = connection.QuerySingleOrDefault <NguoiDung>("SP_DANGNHAP", p, commandType: CommandType.StoredProcedure);
            }
            if (ngDung != null)
            {
                NguoiDungDangNhap nguoiDungDN = new NguoiDungDangNhap
                {
                    MaND     = ngDung.MaND,
                    TaiKhoan = ngDung.TaiKhoan,
                    HoTen    = ngDung.HoTen,
                    DiaChi   = ngDung.DiaChi,
                    Email    = ngDung.Email,
                    NgaySinh = ngDung.NgaySinh,
                    SoDT     = ngDung.SoDT,
                    MaLoaiND = ngDung.LoaiND,
                };
                string accessToken = GenerateToken(nguoiDungDN);
                nguoiDungDN.accessToken = accessToken;
                return(nguoiDungDN);
            }
            var response = await tbl.TBLoi(ThongBaoLoi.Loi500, "Account name or Password is wrong!");

            return(response.Content);
        }
Exemplo n.º 2
0
        private string GenerateToken(NguoiDungDangNhap ndDN)
        {
            var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(
                claims: new Claim[] {
                new Claim(ClaimTypes.Name, ndDN.TaiKhoan),
                new Claim(ClaimTypes.Role, ndDN.MaLoaiND),
            },
                notBefore: new DateTimeOffset(DateTime.Now).DateTime,
                expires: new DateTimeOffset(DateTime.Now.AddMinutes(60)).DateTime,
                signingCredentials: new SigningCredentials(SIGNING_KEY, SecurityAlgorithms.HmacSha256)


                );

            //string token1 = new JwtSecurityTokenHandler().WriteToken(token);
            return(new JwtSecurityTokenHandler().WriteToken(token));
        }