public HttpResponseMessage GetTokenByUserNamePassword(string UserName, string Password)
        {
            ApplicationContext   db     = new ApplicationContext(new SystemUser());
            ApplicationDbContext userdb = new ApplicationDbContext();
            var userinfo = userdb.Users.FirstOrDefault(p => p.UserName == UserName);

            if (userinfo != null)
            {
                var            user      = userinfo;
                PasswordHasher ph        = new PasswordHasher();
                string         hashedpwd = ph.HashPassword(Password);
                var            result    = ph.VerifyHashedPassword(user.PasswordHash, Password);
                if (result.ToString() == "Success")
                {
                    var obj = db.ApiTokens.FirstOrDefault(p => p.T_UsersID == userinfo.Id);
                    if (obj != null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, obj));
                    }
                    else
                    {
                        var token = GenerateToken.GetToken(userinfo.Id);
                        return(Request.CreateResponse(HttpStatusCode.OK, token));
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Password !"));
            }
            return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Username/Password"));
        }
Example #2
0
        public async Task<IActionResult> Post([FromBody] UserResolver model)
        {
            if (string.IsNullOrEmpty(model.Email))
                NotifierError("email empty.");

            var response = new object();

            // Recupera o usuário
            var userApplication = await user.GetByEmail(model.Email);

            // Verifica se o usuário existe
            if (userApplication == null)
                NotifierError("user not found or without permission.");

            else
            {
                // Gera o Token
                model.Email = userApplication.Email.EmailAddress;
                var token = GenerateToken.GetToken(model);

                response = new
                {
                    email = model.Email,
                    name = userApplication.Name.FullName,
                    token = token
                };
            }


            return CustomResponse(response);
        }
        public HttpResponseMessage AuthenticateUser(string UserName, string Password)
        {
            ApplicationDbContext UserDb = new ApplicationDbContext();
            ApplicationContext   db     = new ApplicationContext(new SystemUser());
            var ssoUsers = UserDb.Users.Where(p => p.UserName == UserName).ToList();

            if (ssoUsers != null && ssoUsers.Count > 0)
            {
                var            user      = ssoUsers[0];
                var            roles     = user.Roles;
                PasswordHasher ph        = new PasswordHasher();
                string         hashedpwd = ph.HashPassword(Password);
                var            result    = ph.VerifyHashedPassword(user.PasswordHash, Password);
                if (result.ToString() == "Success")
                {
                    var obj = db.ApiTokens.FirstOrDefault(p => p.T_UsersID == user.Id);
                    if (obj != null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, obj.T_AuthToken));
                    }
                    else
                    {
                        var token = GenerateToken.GetToken(user.Id);
                        return(Request.CreateResponse(HttpStatusCode.OK, token.T_AuthToken));
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Password !"));
            }
            return(Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Username/Password"));
        }
Example #4
0
        public async Task <IHttpActionResult> GetTokenByUserNamePassword(string UserName, string Password)
        {
            ApplicationContext   db     = new ApplicationContext(new SystemUser());
            ApplicationDbContext userdb = new ApplicationDbContext();
            var userinfo = await userdb.Users.FirstOrDefaultAsync(p => p.UserName == UserName);

            if (userinfo != null)
            {
                var            user      = userinfo;
                PasswordHasher ph        = new PasswordHasher();
                string         hashedpwd = ph.HashPassword(Password);
                var            result    = ph.VerifyHashedPassword(user.PasswordHash, Password);
                if (result.ToString() == "Success")
                {
                    var obj = await db.ApiTokens.FirstOrDefaultAsync(p => p.T_UsersID == userinfo.Id);

                    if (obj != null)
                    {
                        return(Ok(obj));
                    }
                    else
                    {
                        var token = GenerateToken.GetToken(userinfo.Id);
                        return(Ok(token));
                    }
                }
                return(BadRequest("Invalid Password !"));
            }
            return(BadRequest("Invalid Username/Password !"));
        }
Example #5
0
        public async Task <IHttpActionResult> AuthenticateUser(string UserName, string Password)
        {
            ApplicationDbContext UserDb = new ApplicationDbContext();
            ApplicationContext   db     = new ApplicationContext(new SystemUser());
            var ssoUsers = await UserDb.Users.FirstOrDefaultAsync(p => p.UserName == UserName);

            if (ssoUsers != null)
            {
                var            user      = ssoUsers;
                var            roles     = user.Roles;
                PasswordHasher ph        = new PasswordHasher();
                string         hashedpwd = ph.HashPassword(Password);
                var            result    = ph.VerifyHashedPassword(user.PasswordHash, Password);
                if (result.ToString() == "Success")
                {
                    var obj = await db.ApiTokens.FirstOrDefaultAsync(p => p.T_UsersID == user.Id);

                    if (obj != null)
                    {
                        return(Ok(obj.T_AuthToken));
                    }
                    else
                    {
                        var token = GenerateToken.GetToken(user.Id);
                        return(Ok(obj.T_AuthToken));
                    }
                }

                return(BadRequest("Invalid Password !"));      //Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Password !");
            }
            return(BadRequest("Invalid Username/Password !")); // Request.CreateResponse(HttpStatusCode.NotFound, "Invalid Username/Password");
        }
        public async Task <ModelResponseMessageRegisterUser> AddRegisterUserAsync(EntityRegisterUser entity_model)
        {
            ModelResponseMessageRegisterUser resp = new ModelResponseMessageRegisterUser();

            var cultureInfo = new CultureInfo("en-GB");

            CultureInfo.DefaultThreadCurrentCulture   = cultureInfo;
            CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

            var LicenseKey = GenerateToken.GetToken();

            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("sp_register_user", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@RegisterId", SqlDbType.VarChar, 100).Value    = DateTime.Now.ToString("yyyyMMdd");
                    cmd.Parameters.Add("@LicenseKey", SqlDbType.NVarChar).Value        = LicenseKey;
                    cmd.Parameters.Add("@UserId", SqlDbType.VarChar, 200).Value        = entity_model.userid;
                    cmd.Parameters.Add("@Passw", SqlDbType.NVarChar, 500).Value        = entity_model.passw;
                    cmd.Parameters.Add("@ConfirmPassw", SqlDbType.NVarChar, 500).Value = entity_model.confirmpassw;
                    cmd.Parameters.Add("@Email", SqlDbType.VarChar, 250).Value         = entity_model.email;
                    cmd.Parameters.Add("@RegisterDate", SqlDbType.DateTime).Value      = entity_model.register_date;
                    cmd.Parameters.Add("@ExpireDate", SqlDbType.DateTime).Value        = entity_model.register_expire;

                    SqlParameter rStatus = cmd.Parameters.Add("@rStatus", SqlDbType.Int);
                    rStatus.Direction = ParameterDirection.Output;
                    SqlParameter rMessage = cmd.Parameters.Add("@rMessage", SqlDbType.NVarChar, 500);
                    rMessage.Direction = ParameterDirection.Output;
                    SqlParameter rRegisterId = cmd.Parameters.Add("@rRegisterId", SqlDbType.VarChar, 100);
                    rRegisterId.Direction = ParameterDirection.Output;

                    await cmd.ExecuteNonQueryAsync();

                    if ((int)cmd.Parameters["@rStatus"].Value > 0)
                    {
                        resp.Status     = true;
                        resp.RegisterId = cmd.Parameters["@rRegisterId"].Value.ToString();
                    }
                    else
                    {
                        resp.Message = cmd.Parameters["@rMessage"].Value.ToString();
                    }
                }
                conn.Close();
            }
            return(resp);
        }
        public HttpResponseMessage GetTokenByUserId(string id)
        {
            ApplicationContext db = new ApplicationContext(new SystemUser());
            var obj = db.ApiTokens.FirstOrDefault(p => p.T_UsersID == id);

            if (obj != null)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, obj));
            }
            else
            {
                var token = GenerateToken.GetToken(id);
                return(Request.CreateResponse(HttpStatusCode.OK, token));
            }
        }
Example #8
0
        public async Task <IHttpActionResult> GetTokenByUserId(string id)
        {
            ApplicationContext db = new ApplicationContext(new SystemUser());
            var obj = await db.ApiTokens.FirstOrDefaultAsync(p => p.T_UsersID == id);

            if (obj != null)
            {
                return(Ok(obj));
            }
            else
            {
                var token = GenerateToken.GetToken(id);
                return(Ok(token));
            }
        }
Example #9
0
        public System.Web.Mvc.ActionResult Index()
        {
            ApplicationDbContext userdb = new ApplicationDbContext();
            ApplicationContext   db     = new ApplicationContext(new SystemUser());
            var userinfo = userdb.Users.FirstOrDefault(p => p.UserName == User.Name);

            if (userinfo != null)
            {
                var tokeninfo = db.ApiTokens.FirstOrDefault(p => p.T_UsersID == userinfo.Id);
                if (tokeninfo != null)
                {
                    ViewData["Token"] = tokeninfo.T_AuthToken;
                }
                else
                {
                    var token = GenerateToken.GetToken(userinfo.Id);
                    ViewData["Token"] = token.T_AuthToken;
                }
            }
            return(View(Configuration.Services.GetApiExplorer().ApiDescriptions));
        }