示例#1
0
        public IActionResult AddConnection(AddConnectionRequest model)
        {
            var loginId = ((Login)HttpContext.Items["Login"]).LoginId;

            //check if user already has registered this connection

            //get all connections from this user
            var connections = ctx.ConnectionTables.ToList().Where(c => c.LoginId == loginId).ToList();

            if (connections.Exists(c => c.Database == model.Database &&
                                   c.Host == model.Host &&
                                   c.SqlPlatformId == Int32.Parse(model.SqlPlatformId) &&
                                   c.Port == model.Port))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Identicall connection already registered."
                }));
            }

            ConnectionTable newConnection = new ConnectionTable
            {
                SqlPlatformId = Int32.Parse(model.SqlPlatformId),
                Database      = model.Database,
                Host          = model.Host,
                LoginId       = loginId,
                Username      = model.Username,
                Port          = model.Port
            };

            ctx.Add(newConnection);
            ctx.SaveChanges();

            return(Ok(new
            {
                error = false,
                message = "Connection successfully created."
            }));
        }
        public async Task <IActionResult> Register(Login login)
        {
            if (string.IsNullOrEmpty(login.Username))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Username must not be empty"
                }));
            }

            if (string.IsNullOrEmpty(login.Email))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Email must not be empty"
                }));
            }

            if (string.IsNullOrEmpty(login.Password))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Password must not be empty"
                }));
            }

            var usernames = await ctx.Logins.Select(l => l.Username).ToListAsync();

            var emails = await ctx.Logins.Select(l => l.Email).ToListAsync();

            if (usernames.Contains(login.Username))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Username already taken"
                }));
            }

            if (emails.Contains(login.Email))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Email already taken"
                }));
            }

            login.Password = BCrypt.Net.BCrypt.HashPassword(login.Password);

            ctx.Add(login);
            await ctx.SaveChangesAsync();


            AuthenticateRequest model = new AuthenticateRequest
            {
                Username = login.Username,
                Password = login.Password
            };

            var response = await _userService.Authenticate(model);

            if (response == null)
            {
                return(BadRequest(new
                {
                    message = "Account registered but could not log in automatically",
                    error = true
                }));
            }

            return(Ok(new
            {
                username = response.Username,
                email = response.Email,
                token = response.Token,
                error = false,
                message = "Successfully registered"
            }));
        }