Esempio n. 1
0
 public void SaveUser(IoTUser user)
 {
     using (var db = new DataContext())
     {
         db.Database.BeginTransaction();
         db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users ON");
         db.Users.Add(user);
         db.SaveChanges();
         db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users OFF");
         db.Database.CommitTransaction();
     }
 }
Esempio n. 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            IoTUser user = null;
            string  username = "", password = "";

            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(':');
                username = credentials[0];
                password = credentials[1];
                var res = _userService.Authenticate(username, password);
                user = new IoTUser()
                {
                    Username = res.Username, Email = res.Email, Id = res.Id
                };
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }
            ;
            if (user == null)
            {
                return(AuthenticateResult.Fail("Invalid Username or password"));
            }

            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Username),
            };
            var identity  = new ClaimsIdentity(claims, Scheme.Name);
            var principal = new ClaimsPrincipal(identity);
            var ticket    = new AuthenticationTicket(principal, Scheme.Name);

            return(AuthenticateResult.Success(ticket));
        }
Esempio n. 3
0
        public IActionResult GetUser([FromRoute] string userId)
        {
            IoTUser user = null;
            int     a;

            if (int.TryParse(userId, out a))
            {
                user = _userLogic.GetUser(a);
            }
            else
            {
                user = _userLogic.GetUser(userId);
            }
            if (user == null)
            {
                return(NotFound());
            }
            return(Ok(user));
        }
Esempio n. 4
0
 public void SaveUser(IoTUser user)
 {
     _dao.SaveUser(user);
 }
Esempio n. 5
0
 public IActionResult PostUser([FromBody] IoTUser user)
 {
     _userLogic.SaveUser(user);
     return(Ok());
 }
Esempio n. 6
0
 public IoTUser Login([FromBody] IoTUser user)
 {
     return(_userLogic.ValidateLogin(user.Username, user.Password));
 }