Login in mvc
In model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using contestmangement.Models;
namespace contestmangement.Areas.Admin.Models
{
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName {get;set;}
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password {get;set;}
[Display(Name = "Remember me?")]
public bool RememberMe {get;set;}
private dbs_contestmanagementEntities db = new dbs_contestmanagementEntities();
public bool IsValid(string _username, string _pwd)
{
IEnumerable < con_admin > enumerable = db.con_admin.Where(m = > m.adm_username == _username && m.adm_password == _pwd);
List < con_admin > user = enumerable.ToList < con_admin > ();
if (user.Count > 0)
{
return true;
}
return false;
}
}
}
Then in login controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using contestmangement.Areas.Admin.Models;
namespace contestmangement.Areas.Admin.Controllers
{
public class AdminLoginController: Controller
{
//
// GET: /Admin/AdminLogin/
public ActionResult LogOn()
{
return View();
}
//
// POST: /Account/LogOn
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (model.IsValid(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "admin");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
//
// GET: /Account/LogOff
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
#region Status Codes
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
// a full list of status codes.
switch (createStatus)
{
case MembershipCreateStatus.DuplicateUserName:
return "User name already exists. Please enter a different user name.";
case MembershipCreateStatus.DuplicateEmail:
return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
case MembershipCreateStatus.InvalidPassword:
return "The password provided is invalid. Please enter a valid password value.";
case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidAnswer:
return "The password retrieval answer provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidQuestion:
return "The password retrieval question provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidUserName:
return "The user name provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.ProviderError:
return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}
#endregion
}
}
Simple LINQ Query with session
string User;
User = Session["username"].ToString();
var teamDetails = from m in db.con_teamdetails where m.tea_teamname == User orderby m.tea_rank select m;
return View(teamDetails.ToList());