示例#1
0
        /// <summary>
        /// Checks if user with given password exists in the database
        /// </summary>
        /// <param name="_username">User name</param>
        /// <param name="_password">User password</param>
        /// <returns>True if user exist and password is correct</returns>
        public bool Authenticate(string _username, string _password, string IP)
        {
            bool authenticated = false;

            using (SmartMarketDB db = new SmartMarketDB())
            {
                try
                {
                    string encodedPassword          = TextEncoding.EncodeString(_password);
                    BusinessEntities.User loginUser = db.Users
                                                      .FirstOrDefault(u => u.UserName == _username && u.Password == encodedPassword && u.Active);

                    if (loginUser == null)
                    {
                        authenticated = false;
                    }
                    else
                    {
                        authenticated = true;
                    }
                }
                catch
                {
                    authenticated = false;
                }
            }
            return(authenticated);
        }
示例#2
0
        //[System.Web.Http.HttpPost]
        public IHttpActionResult Signup([FromBody] BusinessEntities.User user)
        {
            using (SmartMarketDB _db = new SmartMarketDB())
            {
                using (var dbContextTransaction = _db.Database.BeginTransaction())
                {
                    try
                    {
                        string encodedPassword = TextEncoding.EncodeString(user.Password);
                        user.Password = encodedPassword;
                        user          = _db.Users.Add(user);
                        _db.SaveChanges();
                        string roleName = "";
                        switch (user.UserType)
                        {
                        case UserType.Company:
                            roleName = "Company";
                            break;

                        case UserType.Customer:
                            roleName = "Customer";
                            break;

                        case UserType.EStore:
                            roleName = "Admin";
                            break;
                        }
                        if (!string.IsNullOrEmpty(roleName))
                        {
                            var role     = _db.Roles.SingleOrDefault(r => r.Roles == roleName);
                            var userRole = new UserRole
                            {
                                Role = role,
                                User = user
                            };
                            _db.UserRoles.Add(userRole);
                        }
                        _db.SaveChanges();
                        if (user.UserType == UserType.Customer)
                        {
                            var customer = new Customer
                            {
                                User     = user,
                                Gender   = Gender.Male,
                                Nikename = user.UserName
                            };
                            _db.Customers.Add(customer);
                            _db.SaveChanges();
                        }
                        dbContextTransaction.Commit();

                        Business.User _user = new Business.User();
                        return(Json(new { success = true, Message = "", UserId = _user.GetUserId(user.UserName) }));
                    }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();
                        return(Json(new { success = false, Message = ex.Message + ex.StackTrace, UserId = -1 }));
                    }
                }
            }
        }