Yogesh Sharma

Yogesh Sharma

  • NA
  • 144
  • 44.9k

Login API C# Asp.net

Feb 26 2016 12:05 AM
Dear all friends,
 
The following API check userid and passwords that are already defined...
 
But, i want to check it from sql server table....
 
This is My Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API_Demo.Models
{
public class LoginModel
{
public string Acc_Email_ID { get; set; }
public string Password { get; set; }
}
}
This is my controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using API_Demo.Models;
namespace API_Demo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
LoginModel obj = new LoginModel();
return View(obj); //For bind the view
}
[HttpPost]
public ActionResult Index(LoginModel objuserlogin)
{
var display = Userloginvalues().Where(m => m.Acc_Email_ID == objuserlogin.Acc_Email_ID && m.Password == objuserlogin.Password).FirstOrDefault();
if (display != null)
{
ViewBag.Status = "CORRECT UserNAme and Password";
}
else
{
ViewBag.Status = "INCORRECT UserName or Password";
}
return View(objuserlogin);
}
public List<LoginModel> Userloginvalues()
{
List<LoginModel> objModel = new List<LoginModel>();
objModel.Add(new LoginModel { Acc_Email_ID = "user1", Password = "password1" });
return objModel;
}
}
}
This is my view :
@model API_Demo.Models.LoginModel
@{
ViewBag.Title = "Index";
}
<script language="javascript">
function display() {
if ($("#txtusername").attr("value") == "") {
alert("Please Enter your UserName.");
return false;
} else if ($("#txtuserpassword").attr("value") == "") {
alert("Please enter UserPassword.");
return false;
}
alert("Login Successfully");
return true;
}
</script>
<h3>Simple We API Login Form </h3>
@using (Html.BeginForm("Index", "Home"))
{
<table width="40%" cellpadding="1" cellspacing="5">
<tr>
<td colspan="2" style="color:#f00;font-size:larger">@ViewBag.Status</td>
</tr>
<tr>
<td align="right">User Name :</td>
<td>@Html.TextBoxFor(m => m.Acc_Email_ID, new { @style = "width:200px", @id = "txtusername" })</td>
</tr>
<tr>
<td align="right">User Password :</td>
<td>@Html.PasswordFor(m => m.Password, new { @style = "width:200px", @id = "txtuserpassword" })</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Login" title="login" onclick=" showlog();"/>
</td>
</tr>
</table>
}
 
So, in this API i have checked the id and password offline... (i.e. already defined)...
But, i want check userid and password from sql table ...
So, please suggest me how to check from sql table....

Answers (3)