//*******FORM****************************************************
<div class="row">
<div class="col-md-6 col-md-offset-3">
@using (Html.BeginForm("Login", "Kullanicilar", new { ReturnUrl = ViewBag.ReturnUrl }
, FormMethod.Post, new { @class = "form-signin", id = "LoginForm", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Login failed. Check your login details.");
<hr />
<div class="form-group">
<label for="sKullaniciAdi">Kullanıcı Adı:</label>
<input type="text" class="form-control" id="sKullaniciAdiID" name="sKullaniciAdi" required>
@*@Html.TextBoxFor(m => m.KullanıcıAdı, new { @class = "form-control" })*@
@*@Html.ValidationMessageFor(m => m.KullanıcıAdı, "", new { @class = "text-danger" })*@
</div>
<div class="form-group">
<label for="sSifre">Şifre:</label>
<input type="password" class="form-control" id="sSifreID" name="sSifre" required maxlength="5">
@*@Html.PasswordFor(m => m.Şifresi, new { @class = "form-control" })*@
@*@Html.ValidationMessageFor(m => m.Şifresi, "", new { @class = "text-danger" })*@
</div>
<hr />
<button id="btnLogin" type="submit" class="btn btn-lg btn-warning">Giriş Action</button>
<button id="btnLogin2" type="button" class="btn btn-lg btn-danger">Giriş json</button>
}
</div>
</div>
//*****************SCRIPT******************************************
<script>
$(function () {
$('#btnLogin2').click(function (e) {
var jsonObject = {
"sKullaniciAdi": $('#sKullaniciAdiID').val(),
"sSifre": $('#sSifreID').val()
};
$.ajax({
url: "@Url.Action("LoginKontrolJson")",
type: "POST",
data: JSON.stringify(jsonObject),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
if (response == "1")
{
alert("Doğru");
window.location.href = '@Url.Action("Index", "Home")';
}
else
{
alert(response);
}
}
});
});
});
</script>
//****************JSONRESULT*******************************************
public JsonResult LoginKontrolJson(string sKullaniciAdi, string sSifre)
{
var usr = from u in db.tbKullanicilar
where u.KullanıcıAdı == sKullaniciAdi &&
u.Şifresi == sSifre &&
u.Aktif == true
select u;
if (usr.Count() == 1)
{
return Json("1", JsonRequestBehavior.AllowGet);
}
return Json("Hatalı-Kullanıcı/Şifre", JsonRequestBehavior.AllowGet);
}
//************OR ACTIONRESULT HTTPPOST*******************
[HttpPost]
public ActionResult Login(string sKullaniciAdi,string sSifre)
{
var usr = from u in db.tbKullanicilar
where u.KullanıcıAdı == sKullaniciAdi &&
u.Şifresi == sSifre &&
u.Aktif == true select u;
if (usr.Count() ==1)
{
FormsAuthentication.SetAuthCookie(sKullaniciAdi, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login data is incorrect!");
}
return View();
}
Yorumlar
Yorum Gönder