Ejemplo n.º 1
0
        /// <summary>
        /// Sign in a mobile user into the system. The client's userKey to use is stored in the loginresponse.
        /// </summary>
        /// <param name="username">client username.</param>
        /// <param name="password">client password.</param>
        /// <param name="deviceID">The device ID the of the hardware the client is using.</param>
        /// <returns>Returns the outcome of the operation.</returns>
        public LogInResponse MobileSignIn(string username, string password, string deviceID)
        {
            int MobileID;
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                Response r = db.OpenConnection();
                if (r.error)
                    return (LogInResponse)Common.LogError(r.message, Environment.StackTrace, new LogInResponse(r), 0);

                // Get the salt from the database and salt/hash the password.
                string salt;
                r = db.MobileGetSalt(username, out salt);
                if (r.error)
                    return new LogInResponse(r);
                string saltHashPassword = Common.CreatePasswordHash(password, salt);

                // See if the username/password combination is valid.
                // If it is valid, the userkey will be stored in r.message.
                // If it is not valid, r.message will be empty.
                r = db.MobileValidateUsernamePassword(username, saltHashPassword);
                if (r.error)
                    return (LogInResponse)Common.LogError(r.message, Environment.StackTrace, new LogInResponse(r), 0);

                // If the username/password couldn't be found, inform user.
                if (r.message.Trim() == string.Empty)
                {
                    r.error = true;
                    r.message = "Username/Password is incorrect.";
                    return new LogInResponse(r);
                }

                // Get the client ID stored in r.message.
                if (!int.TryParse(r.message.Trim(), out MobileID))
                {
                    r.error = true;
                    r.message = "Exception in MobileSignIn: Unable to parse MobileID from DB!";
                    return (LogInResponse)Common.LogError(r.message, Environment.StackTrace, new LogInResponse(r), 0);
                }

                // Make sure the client is not logged in. RIGHT NOW: JUST DON'T CHECK ANYTHING USEFUL TO ALLOW FOR LOGINS TO OCCUR WHEN LOGGED IN!
                r = MobileCheckStatus(MobileID, "!4", db);
                if (r.error)
                    return new LogInResponse(r);

                r = db.MobileSignIn(MobileID, deviceID);
                if (r.error)
                    return (LogInResponse)Common.LogError(r.message, Environment.StackTrace, new LogInResponse(r), 0);

                // Attempt to change the MobileID into a userKey
                long userKey;
                r = MobileGenerateKey(MobileID, out userKey, db);
                if (r.error)
                    return (LogInResponse)Common.LogError(r.message, Environment.StackTrace, new LogInResponse(r), 0);

                // If there was no error, create a loginResponse with the successful information.
                LogInResponse lr = new LogInResponse();
                lr.result = r.result;
                lr.userKey = userKey;
                User u = new User();
                u.userName = username;
                u.userID = MobileID;
                return lr;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sign in a mobile user into the system. The client's userKey to use is stored in the loginresponse.
        /// </summary>
        /// <param name="username">client username.</param>
        /// <param name="password">client password.</param>
        /// <param name="deviceID">The device ID the of the hardware the client is using.</param>
        /// <returns>Returns the outcome of the operation.</returns>
        public LogInResponse MobileSignIn(string username, string password, string deviceID)
        {
            int MobileID;
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                ExpResponse r = db.OpenConnection();
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));

                // Get the salt from the database and salt/hash the password.
                string salt;
                r = db.MobileGetSalt(username, out salt);
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_CRED_WRONG, 0));
                string saltHashPassword = Common.CreatePasswordHash(password, salt);

                // See if the username/password combination is valid.
                // If it is valid, the userkey will be stored in r.message.
                // If it is not valid, r.message will be empty.
                r = db.MobileValidateUsernamePassword(username, saltHashPassword);
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));

                // If the username/password couldn't be found, inform user.
                if (r.message.Trim() == string.Empty)
                {
                    r.setErMsg(true, Messages.ERR_CRED_WRONG);
                    return new LogInResponse(r);
                }

                // Get the client ID stored in r.message.
                if (!int.TryParse(r.message.Trim(), out MobileID))
                {
                    r.setErMsgStk(true, "Unable to parse MobileID from DB", "MobileSignIn");
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));
                }

                // Make sure the client is not logged in. RIGHT NOW: JUST DON'T CHECK ANYTHING USEFUL TO ALLOW FOR LOGINS TO OCCUR WHEN LOGGED IN!
                bool validStatus;
                r = MobileCheckStatus(MobileID, "!4", db, out validStatus);
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));
                if(!validStatus)
                {
                    r.setErMsg(true, Messages.ERR_STATUS_ALREADY_IN);
                    return new LogInResponse(r);
                }

                r = db.MobileSignIn(MobileID, deviceID);
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));

                // Attempt to change the MobileID into a userKey
                long userKey;
                r = MobileGenerateKey(MobileID, out userKey, db);
                if (r.error)
                    return new LogInResponse(Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, 0));

                // If there was no error, create a loginResponse with the successful information.
                LogInResponse lr = new LogInResponse();
                lr.result = r.result;
                lr.userKey = userKey;
                return lr;
            }
        }