public ActionResult SignUpSucceed(AuthViewModel viewModel)
 {
     //trying to add new user
     if (_authManager.AddUserToDb(viewModel.User))
     {
         viewModel.SessionId = _sessionManager.CreateSession(viewModel.User);
         return(View(viewModel));
     }
     else
     {
         return(RedirectToAction("SignUpFailed"));
     }
 }
        public HttpResponseMessage Register(ToDoUser user)
        {
            RegisterUserResponse response = new RegisterUserResponse();

            //trying to add new user
            if (_authManager.AddUserToDb(user))
            {
                response.SessionId = _sessionManager.CreateSession(user);
                return(Request.CreateResponse(HttpStatusCode.Created, response));
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.Conflict, response));
            }
        }
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                await next?.Invoke(context);

                return;
            }

            var socket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);

            var socketSender = new SocketSender(socket);

            var sid = sessionManager.CreateSession(socketSender);

            await socketSender.SendTextAsync("session", sid.ToString());

            await Receive(socket, async(result, hid, buffer) => {
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    await sessionManager.SocketMessageAsync(sid, hid, buffer);
                }
                else if (result.MessageType == WebSocketMessageType.Close)
                {
                    await sessionManager.CloseSessionAsync(sid);
                }
            });

            await next?.Invoke(context);
        }
        public string Login(string userName, string password)
        {
            ToDoContext dbContext = new ToDoContext();

            _logManager.Debug("Login process started");
            var user = dbContext.Users.Where(u => u.Username == userName).SingleOrDefault();

            _logManager.Debug("User with provided username found");
            if (user != null)
            {
                if (password == user.Password)
                {
                    _logManager.Info("Provided credentials are valid ");
                    string token = Guid.NewGuid().ToString();
                    _sessionManager.SessionToken = token;
                    _sessionManager.CreateSession("User", user);

                    dbContext.Entry <User>(user).State = System.Data.Entity.EntityState.Detached;

                    _logManager.Info("User sucsesfully logged in: " + user.Id + " " + user.Username + " " + user.Password);
                    _logManager.Debug("Login process ended successfully");
                    return(_sessionManager.SessionToken);
                }
                else
                {
                    _logManager.Error("Invalid password");
                    throw new PasswordInvalidException("Invalid password");
                }
            }
            else
            {
                _logManager.Error("User cannot be found");
                throw new UserNameNotFoundException("User cannot be found");
            }
        }
 Guid CreateSession()
 {
     // create odd sessions like processed by an another server - direct in DB
     return(ThreadIndex % 2 == 0 ?
            SessionMgr.CreateSession(UserId).SessionGuid :
            DbManager.CreateSession(UserId).SessionGuid
            );
 }
Example #6
0
        public async Task Handle(T command, CancellationToken ct = default)
        {
            using (var session = await SessionManager.CreateSession(command.Id))
            {
                await Handle(command, session, ct);

                await session.Commit(command);
            }
        }
Example #7
0
        public override Task <bool> FiltsAsync(HttpFilterContext context)
        {
            if (context.ReturnValue is true)
            {
                var user = _userManager.GetUser(context.HttpContext.Request.Query["user"]);

                context.HttpContext.Response.Cookies.Append("session", _sessionManager.CreateSession(user));
            }

            return(Task.FromResult(true));
        }
Example #8
0
        public async Task <Session> CreateSession(Credentials credentials)
        {
            try
            {
                _logger.Debug("CreateSession started");

                var session = await _sessionManager.CreateSession(credentials, "SessionCreateRQ");

                return(session);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                throw;
            }
            finally
            {
                _logger.Debug("CreateSession finished");
            }
        }
Example #9
0
        public async Task <LoginResponse> AuthenticateUser(string login, string password)
        {
            User user = null;

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();

                user = connection
                       .Query <User>(
                    "EXEC [Support-API].[dbo].[SP_Get_User] @Login = @Login",
                    new { Login = login }
                    ).FirstOrDefault();
            }

            LoginResponse response = new LoginResponse
            {
                Success = false,
                Errors  = new List <string>(),
                JWT     = string.Empty,
            };

            if (user == null)
            {
                response.Errors.Add("Invalid Username or Password");
            }
            else
            {
                Hash   currentHash = new Hash(user.Hash);
                string hash        = Hashing.GenerateHash(password, currentHash.iterations, currentHash.salt);

                if (hash == user.Hash)
                {
                    string JwtSecret = _configuration.GetValue <string>("JwtSecret");
                    var    token     = JWT.GenerateToken(user.UUID, user.Login, JwtSecret);

                    int    code       = Generator.RandomNum(111111, 999999);
                    string hashedCode = Hashing.GenerateHash(code.ToString());

                    string emailPlainTemplate = "Hello {0} {1} {2}! Your Verification Code is: {3}";
                    string emailHtmlTemplate  = "<html>" +
                                                "<body>" +
                                                "<div>" +
                                                "<h2>Support App</h3>" +
                                                "<div>Hello {0} {1} {2}!</div>" +
                                                "<div>Your Verification Code is: {3}</div>" +
                                                "</div>" +
                                                "</body>" +
                                                "</html>";

                    string          emailApiKey = _configuration.GetValue <string>("MailApiKey");
                    SingleEmailPost email       = new SingleEmailPost
                    {
                        From_Email    = "*****@*****.**",
                        From_Name     = "Support App",
                        To_Email      = user.Email,
                        To_Name       = $"{user.First_Name} {user.Middle_Name} {user.Last_Name}",
                        Subject       = "Verification Email",
                        Content_Html  = string.Format(emailHtmlTemplate, user.First_Name, user.Middle_Name, user.Last_Name, code),
                        Content_Plain = string.Format(emailPlainTemplate, user.First_Name, user.Middle_Name, user.Last_Name, code),
                    };
                    await Email.SingleEmail(email, emailApiKey);

                    Session session = _sessionManager.CreateSession(user, token, hashedCode);

                    if (token == null || session == null)
                    {
                        response.Errors.Add("Authentication Error, please contact Administrator.");
                    }
                    else
                    {
                        response.Success = true;
                        response.JWT     = token;
                    }
                }
                else
                {
                    response.Errors.Add("Invalid Username or Password");
                }
            }

            return(response);
        }
Example #10
0
 public void OnGet()
 {
     Session = _sessionManager.CreateSession(
         Request.HttpContext.Connection.RemoteIpAddress
         );
 }