Ejemplo n.º 1
0
        public RestApiResult Create(JObject json)
        {
            if (json == null)
            {
                return new RestApiResult {
                           StatusCode = HttpStatusCode.BadRequest
                }
            }
            ;

            User user = User.FromJson(json);

            user.CreatedDate = DateTime.Now;

            if (ClarityDB.Instance.Users.Any(x => x.UserName.ToLower() == user.UserName.ToLower()))
            {
                string errorJson = "{ 'error': 'User name exists' }";

                return(new RestApiResult {
                    StatusCode = HttpStatusCode.Conflict, Json = JObject.Parse(errorJson)
                });
            }

            user.Salt     = passwordHash.CreateSalt();
            user.Password = passwordHash.CreatePasswordHash(user.Password, user.Salt);

            ClarityDB.Instance.Users.Add(user);
            ClarityDB.Instance.SaveChanges();

            return(new RestApiResult {
                StatusCode = HttpStatusCode.OK, Json = user.ToJson()
            });
        }
Ejemplo n.º 2
0
        public async Task <User> Register(User user, string password)
        {
            byte[] passwordHash, passwordSalt;

            _passHash.CreatePasswordHash(password, out passwordHash, out passwordSalt);
            user.EntryCodeHash = passwordHash;
            user.EntryCodeSalt = passwordSalt;

            user.UserKey = _encryptPassword.GenerateKey();

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }
        public RestApiResult Login(JObject json)
        {
            if (json == null || json.Value <string>("username") == null)
            {
                return(new RestApiResult {
                    StatusCode = HttpStatusCode.BadRequest
                });
            }

            string userName = json.Value <string>("username");
            User   user     = ClarityDB.Instance.Users.Where(x => x.UserName == userName).FirstOrDefault();

            if (user == null)
            {
                return(new RestApiResult {
                    StatusCode = HttpStatusCode.NotFound
                });
            }

            if (user.Password != passwordHash.CreatePasswordHash(json.Value <string>("password"), user.Salt))
            {
                return(new RestApiResult {
                    StatusCode = HttpStatusCode.Conflict
                });
            }

            formsAuthentication.SetAuthCookie(userName, false);

            int currentInterval = GetCurrentInterval();

            return(new RestApiResult {
                StatusCode = HttpStatusCode.OK, Json = new JArray()
                {
                    user.ToJson(), currentInterval
                }
            });
        }