I created a login page, but I try to log in, the codes did go through the jQuery. But I have type in the correct password and user id but it keeps give me fail. So can someone help me ? I have tried this method before on another login page, it works but this time, it does not work.
//Web API
[HttpPost]
public IHttpActionResult Login([FromBody]testLogin LoginU)
{
if(HttpContext.Current.Session["user"] != null)
{
return Ok("LoggedIn");
}
else
{
using (Entities dbc = new Entities())
{
LogIn_Result result = dbc.LogIn(LoginU.id, LoginU.password).FirstOrDefault<LogIn_Result>();
user currentUser = null;
if (result != null)
{
DbSet<user> dbs = dbc.users;
IQueryable<user> q = from s in dbs
where s.id == result.id
select s;
user = q.FirstOrDefault<user>();
HttpContext.Current.Session["user"] = user;
return Ok("/Home/Index");
}
else
{
return Ok("Failed");
}
}
}
//jQuery
function performLogin() {
var login = {
userid: $("#TxtUserName").val(),
password: $("#TxtPassword").val()
};
var apiUrl = "/api/HomeAPI";
alert(login.password);
alert(login.userid);
$.ajax({
type: "POST",
url: apiUrl,
dataType: "JSON",
data: login,
success: function (result) {
alert(result);
if (result == "Failed") {
$("#spanMsg").html("OMG FAILLLL");
}
else if (result == "LoggedIn") {
$("#spanMsg").html("Another dude logon already");
}
else {
window.location.href = result;
}
}
});
}
$(document).ready(function () {
$("#BtnLogin").click(function () {
performLogin();
});
});
Stored procedure:
CREATE PROCEDURE [LogIn]
@id int,
@password varchar(50)
AS
SELECT *
FROM [dbo].[user]
WHERE id = @id AND password = HASHBYTES('SHA1', @password)
if you need anything else do inform me.
My DB for user only got id ( int
), one FK ( int
), username ( varchar
) and password ( varbinary
).
In the performLogin(), the variable used must be the same with the database.
function performLogin() {
var login = {
id: $("#TxtUserName").val(),
password: $("#TxtPassword").val()
};
So just change userid to id and it will work already :D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.