Exemplo n.º 1
0
 // GET: api/Users/5
 public HttpResponseMessage Get(int id)
 {
     try
     {
         using (var context = new PicContext())
         {
             var user = context
                 .Users
                 .Single(u => u.Id == id);
             var res = new UserDto
             {
                 Id = user.Id,
                 Email = user.Email,
                 Info = user.Info,
                 Login = user.Login
             };
             return Request.CreateResponse<UserDto>(HttpStatusCode.OK, res);
         }
     }
     catch (InvalidOperationException)
     {
         return Request.CreateResponse(HttpStatusCode.NotFound);
     }
     catch (ArgumentNullException)
     {
         return Request.CreateResponse(HttpStatusCode.BadRequest);
     }
     catch (Exception)
     {
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
     
 }
Exemplo n.º 2
0
 public UserDto Update(UserDto userDto, string cookie)
 {
     var request = new RestRequest();
     request.AddJsonBody(userDto);
     request.AddParameter("Application/Json", userDto, ParameterType.RequestBody);
     request.AddHeader(Headers.SessionId, cookie);
     var response = _client.Put<UserDto>(request);
     return response.Data;
 }
Exemplo n.º 3
0
 public UserDto Create(UserDto userDto, string password)
 {
     var request = new RestRequest(Method.POST);
     request.RequestFormat = DataFormat.Json;
     request.AddJsonBody(userDto);
     request.AddHeader("password", password);
     var response = _client.Post<UserDto>(request);
     if (response.StatusCode == HttpStatusCode.Conflict)
     {
         throw new Exception("Пользователь с указанными данными уже существует");
     }
     return response.Data;
 }
Exemplo n.º 4
0
 // POST: api/Users
 public HttpResponseMessage Post([FromBody]UserDto user)
 {
     try
     {
         var password = Request.Headers.GetValues("password").Single();
         var salt = HashHelper.CreateSalt();
         using (var context = new PicContext())
         {
             if (context.Users.Count(n => n.Email == user.Email) > 0)
             {
                 return Request.CreateResponse<UserDto>(HttpStatusCode.Conflict, new UserDto());
             }
             context.Users.Add(new User
             {
                 Email = user.Email,
                 Hash = HashHelper.ComputeHash(password, salt),
                 Salt = salt,
                 Login = user.Login,
                 Info = user.Info
             });
             context.SaveChanges();
         }
         using (var context = new PicContext())
         {
             var result = context
                 .Users
                 .Single(u => u.Email == user.Email);
             var newUser = new UserDto
             {
                 Id = result.Id,
                 Email = result.Email,
                 Info = result.Info,
                 Login = result.Login
             };
             return Request.CreateResponse<UserDto>(HttpStatusCode.Created, newUser);
         }
     }
     catch (ArgumentNullException)
     {
         return Request.CreateResponse(HttpStatusCode.BadRequest);
     }
     catch (Exception)
     {
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 5
0
 // PUT: api/Users/5
 public HttpResponseMessage Put(int id, [FromBody]UserDto value)
 {
     try
     {
         using (var context = new PicContext())
         {
             var user = context.Users.Single(u => u.Id == id);
             user.Email = value.Email ?? user.Email;
             user.Info = value.Info ?? user.Info;
             context.SaveChanges();
             var r = new UserDto
             {
                 Id = user.Id,
                 Email = user.Email,
                 Info = user.Info,
                 Login = user.Login
             };
             return Request.CreateResponse<UserDto>(HttpStatusCode.Accepted, r);
         }
     }
     catch (InvalidOperationException)
     {
         return Request.CreateResponse(HttpStatusCode.NotFound);
     }
     catch (ArgumentNullException)
     {
         return Request.CreateResponse(HttpStatusCode.BadRequest);
     }
     catch (Exception)
     {
         return Request.CreateResponse(HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 6
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserClient userClient = new UserClient();
                    UserDto userDto = new UserDto() { Email = model.Email, Login = model.Email };
                    var regResult = userClient.Create(userDto, model.Password);
                }
                catch (Exception e)
                {
                    return View("Error",
                        new ErrorViewModel() { Message = e.Message, StatusCode = HttpStatusCode.Conflict });
                }

                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    


                    SessionClient sessionClient = new SessionClient();
                    var cookie = sessionClient.Authorize(model.Email, model.Password);
                    HttpContext.Response.Cookies["sessionId"].Value = cookie;
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }