Ejemplo n.º 1
0
        public JsonResult Login(LoginForm login)
        {
            using (SoHoaEntities db = new SoHoaEntities())
            {
                S_Users user = db.S_Users.SingleOrDefault(x => x.UserName == login.Username);
                if (user != null)
                {
                    string passwordSalt  = user.PasswordSalt;
                    string passwordInput = AuthenticationHelper.GetMd5Hash(passwordSalt + login.Password);
                    string passwordUser  = user.Password;

                    if (passwordInput.Equals(passwordUser))
                    {
                        TokenProvider tokenProvider = new TokenProvider();
                        TokenIdentity token         = tokenProvider.GenerateToken(login.Username,
                                                                                  Request.Headers["User-Agent"].ToString(),
                                                                                  HttpContext.Request.UserHostAddress, Guid.NewGuid().ToString(),
                                                                                  DateTime.Now.AddHours(7).Ticks);
                        token.SetAuthenticationType("Custom");
                        token.SetIsAuthenticated(true);
                        db.AccessTokens.Add(new AccessToken()
                        {
                            Token         = token.Token,
                            EffectiveTime = new DateTime(token.EffectiveTime),
                            ExpiresIn     = token.ExpiresTime,
                            IP            = token.IP,
                            UserAgent     = token.UserAgent,
                            UserName      = token.Name
                        });
                        db.SaveChanges();

                        return(Json(
                                   new
                        {
                            Token = token,
                            Profile = new
                            {
                                Username = token.UserName,
                                FullName = user.UserName,
                            },
                            User = new
                            {
                                UserName = user.UserName,
                                UserId = user.UserID
                            }
                        }));
                    }
                }
            }
            return(Json("Login failed!"));
        }
Ejemplo n.º 2
0
        public IHttpActionResult Login(LoginForm loginForm)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Users user = db.Users.Include(x => x.LoaiTaiKhoan).SingleOrDefault(x => x.UserName == loginForm.Username);
                if (user != null)
                {
                    string passwordSalt  = user.PasswordSalt;
                    string passwordInput = AuthenticationHelper.GetMd5Hash(passwordSalt + loginForm.Password);
                    string passwordUser  = user.Password;

                    if (String.Equals(passwordInput, passwordUser, StringComparison.InvariantCulture) && user.Active == true)
                    {
                        TokenProvider tokenProvider = new TokenProvider();
                        TokenIdentity token         = tokenProvider.GenerateToken(user.UserId, loginForm.Username,
                                                                                  Request.Headers.UserAgent.ToString(),
                                                                                  "", Guid.NewGuid().ToString(),
                                                                                  DateTime.Now.Ticks);
                        token.SetAuthenticationType("Custom");
                        token.SetIsAuthenticated(true);
                        db.AccessTokens.Add(new AccessTokens()
                        {
                            Token         = token.Token,
                            EffectiveTime = new DateTime(token.EffectiveTime),
                            ExpiresIn     = token.ExpiresTime,
                            IP            = token.IP,
                            UserAgent     = token.UserAgent,
                            UserName      = token.Name
                        });
                        db.SaveChanges();
                        return(Ok(
                                   new
                        {
                            AccessToken = token,
                            Profile = new
                            {
                                UserId = user.UserId,
                                Username = user.UserName,
                                Email = user.Email,
                                LoaiTaiKhoanID = user.LoaiTaiKhoanID,
                                LoaiTaiKhoan = user.LoaiTaiKhoan.TenLoai,
                                CoSoID = user.CoSoID
                            }
                        }));
                    }
                }
                return(Ok("Login failed!"));
            }
        }