Benutzerdefiniertes Formular-Authentifizierung / Autorisierung-Schema in ASP.net MVC

Ich versuche, erstellen Sie eine benutzerdefinierte Authentifizierung Schema ASP.NET MVC mit Formular-Authentifizierung. Die Idee, dass ich vielleicht verschiedenen Bereichen auf der Website verwaltet werden - Genehmiger sind und Allgemeine user-Bereich, und diese unterschiedliche login-Seiten und so weiter. Also das ist, was ich will, geschehen.

  1. Benutzer Zugriff auf eingeschränkte Seite (rechts nun, ich habe es geschützt mit einem Kunden AuthorizeAttribute)
  2. Benutzer umgeleitet wird, um eine bestimmte login-Seite (nicht die, die von Web.config).
  3. Benutzer-Anmeldeinformationen überprüft wurden (über die benutzerdefinierte Datenbank-Schema) und Benutzer anmeldet.

Würde wirklich zu schätzen jede Hilfe bei diesem!!!

Dies ist, was ich was ich habe, so weit, und es funktioniert nicht:

 public class AdministratorAccountController : Controller
{
    public ActionResult Login()
    {
        return View("Login");
    }

    [HttpPost]
    public ActionResult Login(AdministratorAccountModels.LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
            if (model.UserName == "admin" && model.Password == "pass") //This will be pulled from DB etc
            {
                var ticket = new FormsAuthenticationTicket(1,               //version 
                                                           model.UserName,  //user name
                                                           DateTime.Now,    //create time
                                                           DateTime.Now.AddSeconds(30), //expire time
                                                           false,           //persistent
                                                           "");             //user data

                var strEncryptedTicket = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strEncryptedTicket);
                Response.Cookies.Add(cookie);

                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

        //If we got this far, something failed, redisplay form
        return View(model);
    }

    [AdministratorAuthorize]
    public ActionResult MainMenu()
    {
        return View();
    }

    public class AdministratorAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var authenCookie = httpContext.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (authenCookie == null) return false;

            var ticket = FormsAuthentication.Decrypt(authenCookie.Value);
            var id = new FormsIdentity(ticket);
            var astrRoles = ticket.UserData.Split(new[] { ',' });
            var principal = new GenericPrincipal(id, astrRoles);
            httpContext.User = principal;
            return true;
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            var model = new AdministratorAccountModels.LoginModel();
            var viewData = new ViewDataDictionary(model);

            filterContext.Result = new ViewResult { ViewName = "Login", ViewData = viewData };

        }
    }
}
InformationsquelleAutor Greg R | 2010-02-24
Schreibe einen Kommentar