示例#1
0
        public IActionResult Index()
        {
            Pomelo.Data.MySql.MySqlConnection con = new Pomelo.Data.MySql.MySqlConnection("");


            return(View());
        }
示例#2
0
        public IActionResult Post([FromBody] Core.EntityLayer.User auth)
        {
            try
            {
                Pomelo.Data.MySql.MySqlConnection sqlAccess_dboUserAccount = new Pomelo.Data.MySql.MySqlConnection(_databaseSettings.Value.ConnectionString);
                Pomelo.Data.MySql.MySqlCommand    dboUserAccountCmd        = new Pomelo.Data.MySql.MySqlCommand {
                };
                dboUserAccountCmd.Connection = sqlAccess_dboUserAccount;
                sqlAccess_dboUserAccount.Open();


                dboUserAccountCmd.CommandText = @"SELECT username, password_hash  
                                                  FROM useraccounts
                                                  WHERE username = '******' AND password_hash = '" + auth.Password + "' ";

                using (Pomelo.Data.MySql.MySqlDataReader dboUserAccountData = dboUserAccountCmd.ExecuteReader())
                {
                    while (dboUserAccountData.Read())
                    {
                        UserName     = dboUserAccountData["username"].ToString().Trim();
                        PasswordHash = dboUserAccountData["password_hash"].ToString().Trim();

                        if (auth.UserName == UserName && auth.Password == PasswordHash)
                        {
                            DateTime?expire      = DateTime.Now.AddMinutes(10);
                            var      tokenString = GetToken(auth.UserName, expire);

                            return(Json(new { auth = true, timestamp = DateTime.Now, status = HttpStatusCode.OK, token = tokenString, tokenExpires = expire }));
                        }
                    }
                    return(this.Json(new { auth = false, timestamp = DateTime.Now, status = HttpStatusCode.Unauthorized, info = "JWT auth fail! Please check username and password." }));
                }
            }
            catch (Exception ex)
            {
                string errorGuid = String.Format(Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 16));

                HttpContext.Session.SetString("ErrorGuid", errorGuid);
                ViewBag.ErrorGuid = HttpContext.Session.GetString("ErrorGuid");

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    string webRootPath = _hostingEnvironment.WebRootPath;


                    using (StreamWriter w = new StreamWriter(webRootPath + "\\log.txt", append: true))
                    {
                        Log.Logging(ex.ToString(), w, ViewBag.ErrorGuid);
                        return(this.Json(new { timestamp = DateTime.Now, errorGuid = ViewBag.ErrorGuid, status = HttpStatusCode.InternalServerError, info = "Something went wrong!" }));
                    }
                }

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    string webRootPath = _hostingEnvironment.WebRootPath;

                    using (StreamWriter w = new StreamWriter(webRootPath + "/log.txt", append: true))
                    {
                        Log.Logging(ex.ToString(), w, ViewBag.ErrorGuid);
                        return(this.Json(new { timestamp = DateTime.Now, errorGuid = ViewBag.ErrorGuid, status = HttpStatusCode.InternalServerError, info = "Something went wrong!" }));
                    }
                }
            }

            return(View());
        }