Firstly, allow me to apologise in advance for my simple understanding of what I am about to ask. I am fairly competent in C#, LinqToSQL, WPF, but am a complete "newbie" at web-development and despite looking at numerous forums and performing countless google searches, I am still at a loss.
I have a requirement to create a user login screen for an MVC3 Rzor Application. My requirement is as simple as this.
- Users are stored in a SQL 2008 database, in a table called users that has the following columns / attributes:
- UserID (int, Primary Key)
- CompanyID (int)
- FirstName (nvarchar 50)
- LastName (nvarchar 50)
- UserName (nvarchar 100) << Will hold e-mail addresses as the user name
- Password (nvarchar 50)
- ActiveUser (bit)
- CanEdit (bit)
- LimitedView (bit)
- LimitedViewLimits (nvarchar MAX)
- CanChangeStatus (bit)
- IsAdmin (bit)
- The MVC application should show the login page initially, authenticate the user, and if successful display the home page. If authentication fails, just show the login page.
- Users should be authenticated by the UserName and Password columns.
- User should be "stored" until browser session closed.
- All other application methods are available once logged in only.
- The LinqToSQL class is called AMDataContext (it's a .dbml file)
The class for the authentication part would be something like:
public User AuthenticateUser(string _username, string _password)
{
using(AMDataContext db = new AMDataContext())
{
User theUser = db.Users
.Where(u => u.UserName == _username)
.Where(u => u.Password == _password)
.SingleOrDefault();
if (theUser == null)
return null;
else
return theUser;
}
}
If null, advise user and diaply login view, if a user is returned, store where the User object can be accessed at any time by other views (namely the home view) and display the home view.
Any help / advice would be greatly appreciated.
Many thanks,
Jon
Shirsendu NandiPosted May 5, 2011, 7:19 AM
Jon See
You can't pass the string under the Http post Logon() action method.
What you can do Like that
[HttpPost]
public ActionResult Logon(LoginClass Employee)
{
}
what You need you have to create a class name LoginClass in your model folder or anywhere and there you define your Required Credentials Properties like uid,password etc.After that put it in to the list like my eg.See the way if any prob then no probs dude..
Shirsendu NandiPosted May 6, 2011, 1:50 AM
jon
I am glad to know that you got your solution...njoy..
Jon BellamyPosted May 5, 2011, 9:02 AM
I've now done just that (until you said I didn't realise the importance of it), and then I passed the Model through to the HomeController Index class (which accepts the model as a parameter) and it works perfectly.
I cannot thank you enough Shirsendu, but thank you again. This has been bugging me for weeks and you've helped me to solve it!
Thank you, Jon
Jon BellamyPosted May 5, 2011, 7:02 AM
Your article is very good, I actually read it earlier in the week before posting on here (all parts).
I decided to read it again after you suggested that, and found it more useful the second time round. I could be wrong, but I think I'm almost there apart from one thing, which perhaps you can point me in the right direction on?
In my LogOn.cshtml file, my submit button calls the LogOn() action in the AccountController class. I need it to call Logon(string _username, string _password) passing the values of the Username and Password input boxes. I'm not entirely sure, but this may complete the process.
How do I do this please?
Thank you for all your help so far, Jon
Shirsendu NandiPosted May 4, 2011, 5:27 AM
http://www.c-sharpcorner.com/UploadFile/b19d5a/6537/
HEre I have uses entity framework.download this article also.
Jon BellamyPosted May 4, 2011, 3:22 AM
OK, so I have the following in my AccountController.cs file:
public class AccountController : Controller
{
// **************************************
// URL: /Account/LogOn
// **************************************
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(string _username, string _password)
{
User theUser = AuthenticateUser(_username, _password);
if (theUser != null)
return RedirectToAction("Index", "Home");
else
return View();
}
public User AuthenticateUser(string _username, string _password)
{
using (AMDataContext db = new AMDataContext())
{
User theUser = db.Users
.Where(u => u.Username == _username)
.Where(u => u.Password == _password)
.SingleOrDefault();
if (theUser == null)
return null;
else
return theUser;
}
}
}
There is a public property on the HomeController class as such:
public User CurrentUser;
I need to pass the returned user from AuthenticateUser(... , ...) to this variable on the Home Controller and display the Home view, if the user returned is null, simply display the login page (preferably with some form of failed notice).
My Action Method - forgive me if I'm telling you wrong - this is where you will see I'm new to MVC and web-development. The following code is in my LogOn.cshtml file (and I believe the action link is now shown in the controller code above):
@{
ViewBag.Title = "Log On";
}
Log On
Please enter your username and password.
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
}
This is just a dumbed-down version of the default one when creating a new MVC3 project.
This is as far as I am, I'm not sure how to built the model, or how to change the above cshhtml file to not need one. I am also unsure of how to pass the User to the HomeController?
Please let me know if you need anything else. Thank you.
Shirsendu NandiPosted May 4, 2011, 1:34 AM