Ejemplo n.º 1
0
        public IHttpActionResult Authenticate([FromBody] LoginRequest login)
        {
            var loginResponse         = new LoginResponse {
            };
            LoginRequest loginrequest = new LoginRequest {
            };

            if (login.Username == null || login.Password == null)
            {
                return(Unauthorized());
            }
            loginrequest.Username = login.Username;
            loginrequest.Password = login.Password;
            string fullname = "";
            int    userId   = -1;

            IHttpActionResult   response;
            HttpResponseMessage responseMsg = new HttpResponseMessage();
            bool isUsernamePasswordValid    = false;

            if (login != null)
            {
                using (RipoffRentalsEntities entities = new RipoffRentalsEntities())
                {
                    var user = entities.Users.FirstOrDefault(u => u.Email == loginrequest.Username);
                    if (user == null)
                    {
                        user = entities.Users.FirstOrDefault(u => u.User_Name == loginrequest.Username);
                        if (user == null)
                        {
                            loginResponse.responseMsg.StatusCode = HttpStatusCode.NotFound;
                            response = ResponseMessage(loginResponse.responseMsg);
                            return(response);
                        }
                    }
                    fullname                = user.Full_Name;
                    userId                  = user.uid;
                    loginrequest.Role       = user.Permissions;
                    isUsernamePasswordValid = loginrequest.Password == user.Password  ? true : false;
                }
            }
            if (isUsernamePasswordValid)
            {
                string token = createToken(loginrequest.Username, userId, fullname, loginrequest.Role);
                loginResponse.Token    = token;
                loginResponse.FullName = fullname;
                loginResponse.Id       = userId;
                return(Ok(loginResponse));
            }
            else
            {
                loginResponse.responseMsg.StatusCode = HttpStatusCode.Unauthorized;
                response = ResponseMessage(loginResponse.responseMsg);
                return(response);
            }
        }
        public async Task <IHttpActionResult> UploadUserPhoto()
        {
            var identity = (ClaimsIdentity)User.Identity;
            int iuid     = Convert.ToInt32(identity.FindFirst("user_id").Value);
            var ctx      = HttpContext.Current;
            var root     = ctx.Server.MapPath("~/App_Data/Images/Users/");
            var provider = new MultipartFormDataStreamProvider(root);


            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (var file in provider.FileData)
                {
                    var lastDot   = file.Headers.ContentDisposition.FileName.LastIndexOf('.');
                    var extension = file.Headers.ContentDisposition.FileName.Substring(lastDot);
                    var name      = iuid.ToString() + extension;

                    name = name.Trim('"');

                    var localFileName = file.LocalFileName;
                    var filePath      = Path.Combine(root, name);

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    File.Move(localFileName, filePath);

                    using (RipoffRentalsEntities db = new RipoffRentalsEntities())
                    {
                        var    currUser = db.Users.First(u => u.uid == iuid);
                        byte[] buffer   = File.ReadAllBytes(filePath);
                        currUser.Image = buffer;
                    }
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }

            return(Ok("File Uploaded successfuly!"));
        }