how to create Register and Login in asp.net mvc.
i want to only logic not all process. please tell me only controller side.
thank you
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajesh GamiPosted Mar 30, 2018, 11:27 PM
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(UserAccount account)
{
if(ModelState.IsValid)
{
using (OurDbContext db = new OurDbContext())
{
db.UserAccount.Add(account);
db.SaveChanges();
}
ModelState.Clear();
ViewBag.Message = account.FirstName + " " + account.LastName + " Successfully Registered ";
}
return View();
}
//==== Login
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(UserAccount user)
{
using (OurDbContext db = new OurDbContext())
{
var usr = db.UserAccount.Single(u => u.UserName == && u.Password == user.Password);
if(usr != null)
{
Session["UserID"] = usr.UserID.ToString();
Session["UserName"] = usr.UserName.ToString();
return RedirectToAction("LoggedIn");
}
else
{
ModelState.AddModelError("", "User Name OR Password is wrong. ");
}
}
return View();
}
public ActionResult LoggedIn()
{
if (Session["UserId"] != null)
{
return View();
}
else
{
return RedirectToAction("Login");
}
}
Bhavesh JadavPosted Mar 30, 2018, 11:38 PM
I have read below article for register and login functionality in one page using MVC. Please refer it.
- https://www.c-sharpcorner.com/article/multiple-models-in-a-single-view-in-asp-net-mvc/#ReadAndPostComment
In above link you can get logic code for login.I hope it's helpful to you.
Thanks
Hiten PandyaPosted Mar 30, 2018, 11:28 PM
Suppose you register yourself in a website then, after successfully getting values of user from your view with validations you need to insert them into database and then get back the latest inserted Id of user in controller for any future processes like to show successful registration message to user.
Now, at the time of login after you get the email or username and password from view to controller.Check that emailId and password combination into database like,
select UserID from tb_User where EmailID = '[email protected]' and password = '123456'
Now if above query returns UserID it means the user is registered and you can set that Id into session for any further processes and show user your dashboard else if you get UserId = 0 menas user is not registered and you can show him view of registration or show some kind of popup with message like you need to register first.
Hope it helps you.
Thanks.
Manav PandyaPosted Mar 30, 2018, 11:27 PM