Ejemplo n.º 1
0
        public object SignIn([FromBody] SignInDTO requestUser)
        {
            try
            {
                using (var context = new TodoAppContext())
                {
                    using (MD5 md5Hash = MD5.Create())
                    {
                        var user = context.users.Where(u => (u.email == requestUser.credential || u.username == requestUser.credential)).FirstOrDefault();

                        if (user == null)
                        {
                            return(NotFound());
                        }

                        string currPassword = GetMd5Hash(md5Hash, requestUser.password);

                        if (!VerifyMd5Hash(md5Hash, requestUser.password, user.password))
                        {
                            return(Request.CreateResponse(HttpStatusCode.NotFound));
                        }
                        var BASIC = CryptographyService.EncryptValue($"{user.username}:{user.password}");

                        var result = new
                        {
                            username = user.username,
                            name     = user.name,
                            lastName = user.lastName,
                            email    = user.email,
                            basicTkn = BASIC
                        };
                        //HttpContext.Current.Response.AppendHeader("Basic", BASIC);
                        //Request.Headers.Add("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user.username}:{user.password}")));
                        return(Request.CreateResponse(HttpStatusCode.OK, result));
                    }
                };
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e));
            }
        }