Exemplo n.º 1
0
        public async Task <ActionResult <ChandlerUser> > LoginAsync([FromBody] AccountDetailsBody details)
        {
            if (details.Username == null && details.Email == null)
            {
                return(this.BadRequest("No username or email has been provided"));
            }
            var user = await this.Helper.FindUserAsync(details.Username, details.Email);

            if (user == null)
            {
                return(this.BadRequest("Username/Email or Password was incorrect"));
            }

            if (user.LockoutEnd > DateTime.Now)
            {
                return(this.StatusCode(429, user));
            }

            var idenres = await this.SignInManager.PasswordSignInAsync(user, details.Password, true, true);

            if (idenres.Succeeded)
            {
                return(this.Ok(user));
            }
            else
            {
                await this.UserManager.AccessFailedAsync(user);

                return(this.BadRequest("Username/Email or Password was incorrect"));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <ChandlerUser> > RegisterAccountAsync([FromBody] AccountDetailsBody details)
        {
            if (string.IsNullOrEmpty(details.Email) || string.IsNullOrEmpty(details.Username) || string.IsNullOrEmpty(details.Password))
            {
                return(this.BadRequest("Parameters cannot be null or empty"));
            }
            var user = await this.Helper.FindUserAsync(details.Username, details.Email);

            if (user != null)
            {
                return(this.BadRequest("Email is already registered"));
            }
            var newusr = new ChandlerUser()
            {
                UserName = details.Username,
                Email    = details.Email
            };
            var idenres = await this.UserManager.CreateAsync(newusr, details.Password);

            if (idenres.Succeeded)
            {
                return(Ok(newusr));
            }
            else
            {
                return(this.StatusCode(500, $"Unable to register new user: {idenres.Errors.First().Description}"));
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> DeleteAccountAsync(ClaimsPrincipal usrclaim, [FromBody] AccountDetailsBody details)
        {
            ChandlerUser user;

            if (usrclaim.Identity != null)
            {
                user = await this.UserManager.GetUserAsync(usrclaim);
            }
            else
            {
                user = await this.Helper.FindUserAsync(details.Username, details.Email);
            }
            if (user == null)
            {
                return(this.BadRequest("User does not exist"));
            }
            var threads = this.Database.Threads.Where(x => x.UserId == user.Id);

            this.Database.Threads.RemoveRange(threads);
            await this.SignInManager.SignOutAsync();

            var res = await this.UserManager.DeleteAsync(user);

            if (res.Succeeded)
            {
                await this.Database.SaveChangesAsync();

                return(this.Ok("Account Deleted"));
            }
            else
            {
                return(this.StatusCode(500, res.Errors.First().Description));
            }
        }