Saturday, July 7, 2012

Adding roles to the 'CreateUserWizard'


Add this to create user table
<tr>
<td align="right">
 <asp:Label ID="RolesLabel" runat="server" Text="Roles" AssociatedControlID="DropDownListRoles"></asp:Label>
 </td>
 <td>
 <asp:DropDownList ID="DropDownListRoles" runat="server">
 </asp:DropDownList>
 </td>
 </tr>

And then this to code
protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("DropDownListRoles");
        roleDropDownList.DataSource = Roles.GetAllRoles();
        roleDropDownList.DataBind();
    }
 
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        DropDownList roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("DropDownListRoles");
        Roles.AddUserToRole(CreateUserWizard1.UserName, roleDropDownList.SelectedValue);
    }

Disable Automatic Login After Registration


The CreateUserWizard has a property called LoginCreatedUser. Just change that to false (Default=true).

Tuesday, May 1, 2012

ASP.net - Validation of viewstate MAC failed

This error is caused when your server is shared


<system.web> 
<machineKey validationKey="{generatedkey}" validation="SHA1" decryption="AES" />
</system.web>

you this url to generate a machineKey

http://www.dcense.com/ASPNETMachineKey.aspx

Monday, April 30, 2012

WebResource.axd Error - "handler must be registered"

In some cases in appears due to corrupt installation of asp.net 4. Just add this


<system.webServer>
   <handlers>
      <add name="WebResource.axd" type="System.Web.Handlers.AssemblyResourceLoader" path="WebResource.axd" verb="GET,HEAD,POST,DEBUG" />
   </handlers>
</system.webServer>

Off you go!

Sunday, April 29, 2012

How to check a Constraint Exception in ASP.net (C#)


try
            {
                //statements
            }
            catch (ConstraintException cex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw;
            }
            /*
             * order matters
            */