I’m new to MVC4 and i’m developing a simple user management application.I’m using entity framework.I’m creating a Admin table in my database.It’s working fine.Previously i created a Internet application project.It also worked fine because Account controller
is creating by it’s own.So now i want to get a proper understand on MVC4.Therefore i’m creating the application from the scratch.Because of that i created a empty application.In my controller i created Login method.But i’m getting this error.
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
Then i checked this issue on internet and got this solution.But it’s also not working.
if (!WebSecurity.Initialized) WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
This is the Login method if my controller.
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(Adminaccount addacc,string returnurl) { if (ModelState.IsValid && WebSecurity.Login(addacc.username, addacc.password)) { return RedirectToAction("Admins"); } ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(addacc); }
Hi,
I think, u need to call WebSecurity.InitializeDatabaseConnection()
under Application_Start() event Handler from global.asax.cs file.
because u r using MVC4 instead of webpages technology.
or, check this site may help u:
http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity%28v=vs.111%29.aspx
Yes i saw this method i have to call.But the issue is although i define this in the global.asax.cs file my application is not working.The error is disappearing.But although i put correct credentials for login.It’s not working.I think my controller’s login
method is correct.
So i just want to create a simple login.Check the user entered username and password whether available in the database and then redirect the user to the relevant page.But in the internet application project i can see a bit complex account controller.
Hi,
To validate the user’s account, you could use WebSecurity.Login method.
If you still have the issue, please share the project on the OneDrive.
Best Regards
Starain