Search code examples
asp.net-mvcasp.net-membership

How to do Membership in ASP.NET MVC?


I want to do membership in ASP.NET MVC. I am not talking about the one that comes with an ASP.NET MVC. project.

I am talking about a database that I have my own custom tables and the aspnet.db tables combined.

I know how to do this in WebForms, and I have combined my tables with the membership stuff, but when I try to add my custom role providers to my web.config like I would in WebForms nothing works.

What is different?


It is so weird. I went back a couple versions in my SVN to a version and added my connection string and changed a couple things around and it works just fine using my database.

When I upload it to Subversion, grab that copy from Subversion 2 seconds later, and load up the solution, it does not work again.

This is the error I am getting:

System.Web.HttpException was unhandled by user code
  Message="Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."
  Source="System.Web"
  ErrorCode=-2147467259
  StackTrace:
       at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
       at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
       at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
       at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)
       at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)
       at System.Web.UI.HiddenFieldPageStatePersister.Load()
       at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
       at System.Web.UI.Page.LoadAllState()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest()
       at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
       at System.Web.UI.Page.ProcessRequest(HttpContext context)
       at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
       at System.Web.Mvc.ViewUserControl.RenderViewAndRestoreContentType(ViewPage containerPage, ViewContext viewContext)
       at System.Web.Mvc.ViewUserControl.RenderView(ViewContext viewContext)
       at System.Web.Mvc.WebFormView.RenderViewUserControl(ViewContext context, ViewUserControl control)
       at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, ViewEngineCollection viewEngineCollection)
       at System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper htmlHelper, String partialViewName)
       at ASP.views_registration_freeedition_aspx.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in i:\StudentPlannerV2\trunk\Current Site\Views\Registration\FreeEdition.aspx:line 23
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
       at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in i:\StudentPlannerV2\trunk\Current Site\Views\Shared\Site.Master:line 53
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Page.Render(HtmlTextWriter writer)
       at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: System.Web.UI.ViewStateException
       Message="Invalid viewstate. \r\n\tClient IP: 127.0.0.1\r\n\tPort: \r\n\tUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)\r\n\tViewState: /wEPDwUKLTE2OTc1MzQyNWRkiTBmkfYmU2SULSGTIy27NmZIwFs=\r\n\tReferer: http://localhost:3668/Registration/FreeEdition\r\n\tPath: /Registration/FreeEdition"
       IsConnected=true
       Path="/Registration/FreeEdition"
       PersistedState="/wEPDwUKLTE2OTc1MzQyNWRkiTBmkfYmU2SULSGTIy27NmZIwFs="
       Referer="http://localhost:3668/Registration/FreeEdition"
       RemoteAddress="127.0.0.1"
       RemotePort=""
       UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"
       InnerException: System.Web.HttpException
            Message="Unable to validate data."
            Source="System.Web"
            ErrorCode=-2147467259
            StackTrace:
                 at System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength)
                 at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
            InnerException:

Solution

  • Disabling the enableViewStateMac setting in the web.config should get rid of that "Validation of viewstate MAC failed" error.

    Set it like this....

    <system.web>
      <pages enableViewStateMac="false" />
    </system.web>
    

    Hope that helps. Good luck.