【发布时间】:2020-08-15 10:04:38
【问题描述】:
我正在使用 Web API 和 ASP.NET MVC。
我在地址栏中传递了错误的用户名和密码,然后也给出了问题所在的成功消息
我的表中没有这条记录:
我正在创建 2 个项目,一个用于 Web API 框架脚手架,第二个是一个普通的空 ASP.NET MVC 项目。
普通的空 ASP.NET MVC 项目(不使用实体框架)
globalvariable.cs:
namespace Mvc
{
public static class globalvariable
{
public static HttpClient webapiclient = new HttpClient();
static globalvariable()
{
webapiclient.BaseAddress = new Uri("https://localhost:0000/api/");
webapiclient.DefaultRequestHeaders.Clear();
webapiclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
}
empsController.cs:
namespace Mvc.Controllers
{
public class empsController : Controller
{
public JsonResult Index()
{
IEnumerable<mvcempmodel> empList;
HttpResponseMessage response = globalvariable.webapiclient.GetAsync("emps").Result;
empList = response.Content.ReadAsAsync<IEnumerable<mvcempmodel>>().Result;
//return View(empList);
return Json(empList, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult Login(string username, string password)
{
if (username == "" || username == null)
{
var data = new
{
message = "Enter Username ",
};
return Json(data, JsonRequestBehavior.AllowGet);
}
else if (password == "" || password == null)
{
var data = new
{
message = "Enter Password",
};
return Json(data, JsonRequestBehavior.AllowGet);
}
else
{
var userdata = "select * from emp where username='" + username + "'and password='" + password + "'".First();
if (userdata != null)
{
var data = new
{
message = "Success",
data = new { username }
};
return Json(data, JsonRequestBehavior.AllowGet);
}
else
{
var data = new
{
message = "Username and Password incorrect ",
};
return Json(data, JsonRequestBehavior.AllowGet);
}
}
}
}
}
}
Web API 项目(使用实体框架)
empsController.cs:
namespace WebApi.Controllers
{
public class empsController : ApiController
{
private empdbEntities db = new empdbEntities();
// GET: api/emps
public IQueryable<emp> Getemps()
{
return db.emps;
}
// GET: api/emps/5
[ResponseType(typeof(emp))]
public IHttpActionResult Getemp(int id)
{
emp emp = db.emps.Find(id);
if (emp == null)
{
return NotFound();
}
return Ok(emp);
}
}
}
我想要什么如果用户输入真实凭据然后给出成功消息,当用户输入错误凭据然后给出用户名和密码不正确的消息
这里我认为我的查询是错误的
var userdata = "select * from emp where username='" + username + "'and password='" + password + "'".First();
请帮忙
【问题讨论】:
-
你不是在查询数据库,你所做的只是定义一个字符串。此外,您通过不安全的查询字符串传递用户名和密码。您还应该使用参数化查询而不是替换
-
@OleEHDufour 我是初学者如何查询数据库,请帮助
标签: c# asp.net-mvc asp.net-web-api