简体   繁体   中英

ASP.Net Membership (Visual Studio 2012 & Web Forms)

I'm using the Membership Provider (Precursor to Identity) in a WebForms Project. I was able to get the users working right out of the box. I registered a few users and ASP.Net created the following tables in my database.

dbo.Memberships
dbo.Profiles
dbo.Rules
dbo.Users
dbo.UsersInRoles
dbo.UsersOpenAuthAccounts
dbo.UsersOpenAuthData

I'm pretty sure that is the entire group of tables required for Membership. I have successfully Restricted access to my site to logged in users. I have created Roles and started to get sections of the site DEFINED IN THE PAGES ITSELF restricted to users with certain roles.

MY PROBLEM IS ADDING ADDITIONAL FIELDS FOR THE USER.

So far I have went the route of simply adding the additional info to the profiles section of my web.config file. I currently have the following defined in my web.config:

<authentication mode="Forms">
  <forms loginUrl="/Account/Login.aspx" timeout="2880" defaultUrl="~/Projects.aspx" />
</authentication>
<authorization>
  <deny users="?" />
  <deny roles="Admin" />
</authorization>
<profile defaultProvider="SqlProvider">
  <providers >
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
  <properties>
    <add name="fname" />
    <add name="lname" />
    <add name="skype" />
    <add name="phone" />
  </properties>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
  <providers>
    <add connectionStringName="DefaultConnection" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </providers>
</roleManager>

So with that in place and the main change being that I added the following in order to try and add additional user related fields:

<profile defaultProvider="SqlProvider">
  <providers >
    ...
  </providers>
  <properties>
    <add name="fname" />
    <add name="lname" />
    <add name="skype" />
    <add name="phone" />
  </properties>
</profile>

I also updated the Accounts/Register.aspx page and it now looks like this:

<%@ Page Title="Register" Language="C#" MasterPageFile="~/DashUser.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="ArchiTrak.Account.Register" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="BodyContent">

        <h2 class="form-signin-heading raleway"><%: Title %>.</h2>

<asp:CreateUserWizard runat="server" ID="RegisterUser" ViewStateMode="Disabled" OnCreatedUser="RegisterUser_CreatedUser">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="wizardStepPlaceholder" />
        <asp:PlaceHolder runat="server" ID="navigationPlaceholder" />
    </LayoutTemplate>
    <WizardSteps>
        <asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
            <ContentTemplate>

            <div class="container">

                <div class="row">
                    <div class="col-md-6">
                    <p class="raleway">
                        Passwords are required to be a minimum of <%: Membership.MinRequiredPasswordLength %> characters in length.
                    </p>

                    <p class="validation-summary-errors">
                        <asp:Literal runat="server" ID="ErrorMessage" />
                    </p>

                    <asp:TextBox class="form-control" runat="server" ID="UserName" placeholder="User Name"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
                        CssClass="field-validation-error" ErrorMessage="The user name field is required." />

                    <asp:TextBox class="form-control" runat="server" ID="fname" placeholder="First Name"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="fname"
                        CssClass="field-validation-error" ErrorMessage="The First Name field is required." />

                    <asp:TextBox class="form-control" runat="server" ID="lname" placeholder="Last Name"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="lname"
                        CssClass="field-validation-error" ErrorMessage="The Last Name field is required." />

                    <asp:TextBox class="form-control" runat="server" ID="phone" placeholder="Phone Number"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="phone"
                        CssClass="field-validation-error" ErrorMessage="The Phone field is required." />

                    <asp:TextBox class="form-control" runat="server" ID="skype" placeholder="Skype Account"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="skype"
                        CssClass="field-validation-error" ErrorMessage="The Skype field is required." />


                    <asp:TextBox class="form-control" runat="server" ID="Email" TextMode="Email" placeholder="Email"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
                        CssClass="field-validation-error" ErrorMessage="The email address field is required." />


                    <asp:TextBox class="form-control" runat="server" ID="Password" TextMode="Password" placeholder="Password"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                        CssClass="field-validation-error" ErrorMessage="The password field is required."/>


                    <asp:TextBox class="form-control" runat="server" ID="ConfirmPassword" TextMode="Password" placeholder="Password"/>
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
                            CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The confirm password field is required." />
                    <asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
                                     CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />

                    <br />
                    <asp:Button runat="server" CommandName="MoveNext" Text="Register" CssClass="btn btn-lg btn-primary btn-block"/>
                    </div>

                </div>
            </div>

            </ContentTemplate>
            <CustomNavigationTemplate />
        </asp:CreateUserWizardStep>
    </WizardSteps>
</asp:CreateUserWizard>
</asp:Content>

I have added the additional fields to collect the first & lastname, skype and phone number. But when I submit the Registration for a new user Nothing shows up in the profiles table in the database.

The Register.aspx.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;

namespace ArchiTrak.Account
{
    public partial class Register : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (!OpenAuth.IsLocalUrl(continueUrl))
            {
                continueUrl = "~/Projects.aspx";
            }
            Response.Redirect(continueUrl);
        }
    }
}

From what I read about the profile way of adding additional information is that it just worked with minimal modifications similar to what I have done above. AM I Missing something? Are there any GOOD tutorials that step by step go over this using Web Forms? I have been searching for information and tutorials So I have found the microsoft links on Roles & Profiles. That s how I got this far. I'm use to Identity, but cannot use it in this project. Any help would be appreciated, or just a kick in the right direction.

As IrishChieftain stated, there was an problem with Profile in Web Application Projects.

Many people doesn't like Profile, because it stored as XML file and you cannot query them easily.

Instead we create separate table to store Custom Profile. You can also look at TableProfileProvider .

For example,

在此处输入图片说明

If you are developing new application, you might want to look at ASP.Net Identity 2 which allows custom columns in User table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM