Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to sign in the DJ using the given credentials.
        /// If an error occurs, the LogInResponse will have the error field as true, and the error will be in message.
        /// </summary>
        /// <param name="username">Username to sign in with.</param>
        /// <param name="password">Password to sign in with.</param>
        /// <returns>LogInReponse returns the outcome. The UserKey sent back is used for all communicaiton in further methods.</returns>
        /// 
        public LogInResponse DJSignIn(string username, string password)
        {
            int DJID = -1;
            using (DatabaseConnectivity db = new DatabaseConnectivity())
            {
                // Try to establish a database connection
                Response r = db.OpenConnection();
                if (r.error)
                    return new LogInResponse(r);

                // Get the salt from the database and salt/hash the password.
                string salt;
                r = db.DJGetSalt(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 DJID will be stored in r.message.
                // If it is not valid, r.message will be empty.
                r = db.DJValidateUsernamePassword(username, saltHashPassword);
                if (r.error)
                    return new LogInResponse(r);

                // 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 DJID stored in r.message.
                if (!int.TryParse(r.message.Trim(), out DJID))
                {
                    r.error = true;
                    r.message = "Exception in DJSignIn: Unable to parse DJID from DB!";
                    return new LogInResponse(r);
                }

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

                // Information seems valid, attempt to sign in.
                r = db.DJSetStatus(DJID, 1);
                if (r.error)
                    return new LogInResponse(r);

                // Attempt to change the DJID into a userKey
                long userKey;
                r = DJGenerateKey(DJID, out userKey, db);
                if (r.error)
                    return new LogInResponse(r);

                // 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 = DJID;
                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;
            }
        }