コード例 #1
0
 public IEnumerable <Models.Employee> GetAllEmployees()
 {
     using (var context = new WebAPIDemoDBEntities())
     {
         return(MapFromDAL(context.Employees.ToList()));
     }
 }
コード例 #2
0
 public IHttpActionResult Get()
 {
     using (var context = new WebAPIDemoDBEntities())
     {
         return(Ok(MapFromDAL(context.Employees.ToList())));
     }
 }
コード例 #3
0
        // GET api/<controller>
        public async Task <IHttpActionResult> Get()
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employees = await context.Employees.ToListAsync();

                return(Ok(MapFromDAL(employees)));
            }
        }
コード例 #4
0
        /// <summary>
        /// USage:
        /// Make a post request to
        /// http://[Server]:[port]/token
        /// Body type: x-www-form-urlencoded
        /// Key - Values
        /// username - [username]
        /// password - [password]
        /// grant_type - password
        /// </summary>
        /// <param name="context"></param>
        /// <returns>
        /// {
        ///     "access_token": [actual token here],
        ///     "token_type": "bearer",
        ///     "expires_in": 1199
        /// }
        /// </returns>
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            //var res = context.OwinContext.Response;
            //res.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            //res.Headers.Add("Access-Control-Allow-Methods", new[] { "HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS" });
            //res.Headers.Add("Access-Control-Allow-Headers", new[] { "Origin", "Content-Type", "X-Auth-Token" });


            //Models.Employee authenticatedEmployee = new EmployeeService().Authenticate(context.UserName, context.Password);

            //    if (authenticatedEmployee == null)
            //    {
            //        return BadRequest("Username or password is incorrect");
            //    }

            using (var db = new WebAPIDemoDBEntities())
            {
                if (db != null)
                {
                    var empl = db.Employees.ToList();
                    //var user = db.Users.ToList();
                    if (empl != null)
                    {
                        if (!string.IsNullOrEmpty(empl.Where(u => u.Username == context.UserName && u.Password == context.Password).FirstOrDefault().Name))
                        {
                            identity.AddClaim(new Claim("Age", "16"));

                            var props = new AuthenticationProperties(new Dictionary <string, string>
                            {
                                {
                                    "userdisplayname", context.UserName
                                },
                                {
                                    "role", "admin"
                                }
                            });

                            var ticket = new AuthenticationTicket(identity, props);
                            context.Validated(ticket);
                        }
                        else
                        {
                            context.SetError("invalid_grant", "Provided username and password is incorrect");
                            context.Rejected();
                        }
                    }
                }
                else
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    context.Rejected();
                }
                return;
            }
        }
コード例 #5
0
 public IHttpActionResult GetEmployee(int id)
 {
     using (var context = new WebAPIDemoDBEntities())
     {
         var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
         if (employee == null)
         {
             return(NotFound());
         }
         return(Ok(MapFromDAL(employee)));
     }
 }
コード例 #6
0
        // GET api/<controller>/5
        public async Task <IHttpActionResult> Get(int id)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employees = await context.Employees.ToListAsync();

                var employee = employees.FirstOrDefault((p) => p.Id == id);
                if (employee == null)
                {
                    return(NotFound());
                }
                return(Ok(MapFromDAL(employee)));
            }
        }
コード例 #7
0
        public IHttpActionResult UpdateEmployee(Models.Employee emp)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == emp.Id);
                employee = MapToDAL(emp, employee);

                context.SaveChanges();

                //var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                //if (employee == null)
                //{
                //    return NotFound();
                //}
                return(Ok(employee));
            }
        }
コード例 #8
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (var db = new WebAPIDemoDBEntities())
            {
                if (db != null)
                {
                    var empl = db.Employees.ToList();
                    //var user = db.Users.ToList();
                    if (empl != null)
                    {
                        if (!string.IsNullOrEmpty(empl.Where(u => u.Username == context.UserName && u.Password == context.Password).FirstOrDefault().Name))
                        {
                            identity.AddClaim(new Claim("Age", "16"));

                            var props = new AuthenticationProperties(new Dictionary <string, string>
                            {
                                {
                                    "userdisplayname", context.UserName
                                },
                                {
                                    "role", "admin"
                                }
                            });

                            var ticket = new AuthenticationTicket(identity, props);
                            context.Validated(ticket);
                        }
                        else
                        {
                            context.SetError("invalid_grant", "Provided username and password is incorrect");
                            context.Rejected();
                        }
                    }
                }
                else
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    context.Rejected();
                }
                return;
            }
        }
コード例 #9
0
        //[Route("api/Employees/Login")]
        //[HttpPost]
        //public IHttpActionResult Login([FromBody] Models.Employee emp)
        //{
        //    return Ok("Reacher here");
        //}

        // POST api/<controller>
        //public void Post([FromBody]string value)
        //{
        //    string s = value;
        //}

        // PUT api/<controller>/5
        //public void Put(int id, [FromBody]Models.Employee emp)
        //{
        //    string text = emp.Name;
        //}

        // DELETE api/<controller>/5
        public async Task <IHttpActionResult> Delete(int id)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                if (employee == null)
                {
                    return(NotFound());
                }
                else
                {
                    context.Employees.Remove(employee);
                    await context.SaveChangesAsync();

                    return(Ok(MapFromDAL(employee)));
                }
            }
        }
コード例 #10
0
        // POST api/<controller>
        //public void Post([FromBody]string value)
        //{
        //    string s = value;
        //}

        // PUT api/<controller>/5
        //public void Put(int id, [FromBody]Models.Employee emp)
        //{
        //    string text = emp.Name;
        //}

        // DELETE api/<controller>/5
        public IHttpActionResult Delete(int id)
        {
            using (var context = new WebAPIDemoDBEntities())
            {
                var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                if (employee == null)
                {
                    return(NotFound());
                }
                else
                {
                    context.Employees.Remove(employee);
                    context.SaveChanges();

                    return(Get());
                }
            }
        }
コード例 #11
0
        public async Task <IHttpActionResult> PostEmployee([FromBody] Models.Employee emp)
        {
            //string s = employee.Name;
            //return Ok(employee);

            using (var context = new WebAPIDemoDBEntities())
            {
                WebAPI_DB.Employee employee = null;
                if (emp.Id == 0) //Add employee
                {
                    employee = new WebAPI_DB.Employee();
                    employee = MapToDAL(emp, employee);
                    //employee.Id = DBNull.Value;

                    context.Employees.Add(employee);
                }
                else
                {
                    employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == emp.Id);
                    employee = MapToDAL(emp, employee);
                }

                await context.SaveChangesAsync();

                //var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                //if (employee == null)
                //{
                //    return NotFound();
                //}
                return(Ok(employee));
            }

            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}
            //_context.employee.Add(employee);
            //await _context.SaveChangesAsync();
            //return CreatedAtAction("GetEmployee", new
            //{
            //    id = employee.ID
            //}, employee);
        }
コード例 #12
0
        //private DataContext _context;

        //public UserService(DataContext context)
        //{
        //    _context = context;
        //}

        public Models.Employee Authenticate(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            using (var context = new WebAPIDemoDBEntities())
            {
                var emp = context.Employees.SingleOrDefault(x => x.Username == username && x.Password == password);

                // check if user exists
                if (emp == null)
                {
                    return(null);
                }

                // authentication successful
                return(MapFromDAL(emp));
            }
        }