示例#1
0
 public ActionResult LoginWithFacebook(FbCredentials userCred)
 {
     try
     {
         if (userCred == null
             || ExtensionMethods.IsEmptyOrSpace(userCred.FbId)
             || ExtensionMethods.IsEmptyOrSpace(userCred.FbAccessToken))
             //  return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param"));
             return null;
         User user = _userManager.LoginWithFacebook(userCred);
         if (user == null)
            // return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed."));
             return null;
         else
         {
             UserLite lite = new UserLite();
             lite.CopyFromUserIncludeCredentials(user);
             UserCreationResponse result = new UserCreationResponse
             {
                 UserId = user.UserId,
                 Token = user.Credential.SecurityToken,
                 User = lite
             };
            // return Request.CreateResponse(HttpStatusCode.OK, result);
             return null;
         }
     }
     catch (ParamMissingException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message));
     }
     catch (AlreadyExistsException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.Conflict, GetErrorResponse(e.Message));
     }
     catch (InvalidValueException e)
     {
         return null;
        // return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse(e.Message));
     }
     catch (UserNotFoundException e)
     {
         return null;
         //return Request.CreateResponse(HttpStatusCode.NotFound, GetErrorResponse(e.Message));
     }
     catch (Exception e)
     {
         return null;
         //return Request.CreateResponse(HttpStatusCode.InternalServerError, GetErrorResponse("Oops, server encountered an issue... " + e.Message));
     }
 }
示例#2
0
        public HttpResponseMessage PostRegisterWithUsername(UserLite user)
        {
            try
            {
                /*
                 * 1. Check if all required fields are present.
                 * 2. Take hash of Password.
                 * 3. Make gender lower case.
                 * 4. Check if username is already taken.
                 * 5. Check if email is already registered.
                 * 6. If not, create a new user and return its ID to user.
                 *
                 */
                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable,
                        GetErrorResponse("Missing required param."));

                if (
                    //|| ExtensionMethods.IsEmptyOrSpace(user.FirstName)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.LastName)
                    //||
                    ExtensionMethods.IsEmptyOrSpace(user.RoleId)
                    || user.Credentials == null
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Email)
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Username)
                    || ExtensionMethods.IsEmptyOrSpace(user.Credentials.Password)
                    //|| ExtensionMethods.IsEmptyOrSpace(user.Credentials.MobileNumber)
                    )
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param."));

                user.Credentials.Password = ExtensionMethods.ToSHA1EncodedString(user.Credentials.Password);
                user.Credentials.Username = user.Credentials.Username.Trim().ToLower();

                if (user.Credentials.Email.IsValidEmail() == false)
                    return Request.CreateResponse(HttpStatusCode.BadRequest, GetErrorResponse("Incorrect email address provided."));
                // Check if already signed up.
                if (_userManager.EmailExists(user.Credentials.Email))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Email already registered."));

                if (_userManager.UsernameExists(user.Credentials.Username))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Username already registered."));

                User savedUser = _userManager.AddUser(user);
                UserLite lite = new UserLite();
                lite.CopyFromUserIncludeCredentials(savedUser);
                UserCreationResponse result = new UserCreationResponse
                {
                    UserId = savedUser.UserId,
                    Token = savedUser.Credential.SecurityToken,
                    User = lite
                };
                return Request.CreateResponse(HttpStatusCode.OK, result);
            }catch (ParamMissingException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse { Message = e.Message });
            }catch (AlreadyExistsException e){
                return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message });
            }catch (InvalidValueException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            }catch (UserNotFoundException e){
                return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message });
            }catch (Exception e){
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse{ Message = "Oops, server encountered an issue... " + e.Message});
            }
        }
示例#3
0
 public HttpResponseMessage VerifyUser()
 {
     try
     {
         /*
          * This method is called when user launches their app.
          * It should give back user info, including pending requests and all.
          * Right now lets just reutrn the User as other things are not implemneted yet.
          * BUT REMEMBER TO UPDATE THIS AS FUNCTIONALITY GROWS.
          *
          */
         UserLite lite = new UserLite();
         // credentials are loaded in basic authentication along with user.
         // just use that object.
         lite.CopyFromUserIncludeCredentials(this.User.User);
         return Request.CreateResponse(HttpStatusCode.OK, lite);
     }
     catch (ParamMissingException e){
         return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
     } catch (AlreadyExistsException e){
         return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message });
     }catch (InvalidValueException e){
         return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
     }catch (UserNotFoundException e){
         return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message });
     }catch (Exception e){
         return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse { Message = "Oops, server encountered an issue... " + e.Message });
     }
 }
示例#4
0
        public HttpResponseMessage Login(CredentialsLite userCred)
        {
            try
            {
                if (userCred == null
                    //|| ExtensionMethods.IsEmptyOrSpace(userCred.Email)
                    || (ExtensionMethods.IsEmptyOrSpace(userCred.Username)
                    && ExtensionMethods.IsEmptyOrSpace(userCred.Email))
                    || ExtensionMethods.IsEmptyOrSpace(userCred.Password))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param"));

                userCred.Password = ExtensionMethods.ToSHA1EncodedString(userCred.Password);
                //userCred.Email = userCred.Email.Trim().ToLower();
                if(string.IsNullOrWhiteSpace(userCred.Username) == false)
                    userCred.Username = userCred.Username.Trim().ToLower();
                if (string.IsNullOrWhiteSpace(userCred.Email) == false)
                    userCred.Email = userCred.Email.Trim().ToLower();
                User user = _userManager.Login(userCred);
                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed."));
                else
                {
                    UserLite lite = new UserLite();
                    lite.CopyFromUserIncludeCredentials(user);
                    UserCreationResponse result = new UserCreationResponse
                    {
                        UserId = user.UserId,
                        Token = user.Credential.SecurityToken,
                        User = lite
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }
            }
            catch (ParamMissingException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            } catch (AlreadyExistsException e){
                return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message });
            }catch (InvalidValueException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            }catch (UserNotFoundException e){
                return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message });
            }catch (Exception e){
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse { Message = "Oops, server encountered an issue... " + e.Message });
            }
        }