I want to implement a login system, but I dont want the Membership provider. My reason, is that the membership provider want to control to must and it is useing prename table. And I dont like it!
So I search the internet for a more simpel why of making a login system.
My solution is IPrincipal, FormsIdentity witch give me the power og both worlds, the buildin DataAnotations and a simpel login system.
My problom is, that I cant det it to work :D
* The redirect
* The registion og a system - by a ticket
* And what I have mist
I Am useing MVC.
My struckture in my projekt
/Abstract - All use may have access to this folder also, and all subfolders
/Abstract/Helpers
/Management - If we need to access this part of the website, it will need a authentication
/Management/Views
/Management/Controller
/Website - All users have access to this part of the website,and all subfolders
/Website/Views
/Website/Controller
My Web.Config - I set all users to have access - I need to authcaticate and so on, on the controllers
<authentication mode="Forms">
<forms loginUrl="~/Management/Account/Login" name="AuthCookie" timeout="60" cookieless="AutoDetect"
path="/">
</forms>
</authentication>
<authorization>
<allow users="*" />
</authorization>
I have constructed a provider class, that the AccountController is using insted of the Membership provider.
My source is this: http://msdn.microsoft.com/en-us/library/aa302401.aspx
In my Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
new App.Providers.AuthenticationProvider().Application_AuthenticateRequest();
}
In my AccountController
private ActionResult DoLogin(string username, string password, bool rememberMe, string returnUrl)
{
//ViewData["Title"] = "Login";
// Basic parameter validation
if (String.IsNullOrEmpty(username))
{
ModelState.AddModelError("username", "You must specify a username.");
}
if (String.IsNullOrEmpty(password))
{
ModelState.AddModelError("password", "You must specify a password.");
}
if (ViewData.ModelState.IsValid)
{
bool loginSuccessful = _provider.ValidateUser(username, password); /* Check if the password and username is OK */
if (loginSuccessful)
{
_provider.SetAuthCookie(username, false);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl); /* HERE: return is always NULL */
}
else
{
return RedirectToAction("Home", "Management", new { module = "Management" });
}
}
else
{
ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
ViewData["rememberMe"] = rememberMe;
return View("Login");
}
In my provider class, I set the user and cache him.
public partial class AuthenticationProvider
{
private AccountService _accountService;
public AuthenticationProvider()
{
this._accountService = new AccountService();
}
public AuthenticationProvider(AccountService accountService)
{
this._accountService = accountService;
}
public bool ValidateUser(string userName, string passWord)
{
return _accountService.ValidateUser(userName, passWord);
}
public void Application_AuthenticateRequest()
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get Forms Identity From Current User
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
// Get Forms Ticket From Identity object
FormsAuthenticationTicket ticket = id.Ticket;
// Create a custom Principal Instance and assign to Current User (with caching)
MyPrincipal principal = (MyPrincipal)HttpContext.Current.Cache.Get(id.Name);
if (principal == null)
{
// Create and populate your Principal object with the needed data and Roles.
List<string> roles = _accountService.GetRoles(id.Name);
principal = new MyPrincipal(id, roles);
HttpContext.Current.Cache.Add(
id.Name,
principal,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0, 30, 0),
System.Web.Caching.CacheItemPriority.Default,
null);
HttpContext.Current.User = principal;
}
}
}
}
}
public void SetAuthCookie(string username, bool createPersistentCookie)
{
FormsAuthentication.SetAuthCookie(username, createPersistentCookie);
}
public void SignOut()
{
FormsAuthentication.SignOut();
}
}
Nothing of all this, seens to work.
I have set the
[Authorize(Roles = "admin", Order = 1)]
On one of the Controllers, on the top class.
thx
// Dennis Larsen