I'm sure this is an old topic, but I'm coming up blank. It seems archaic to me that any site would still try to use unique usernames as credentials rather than an email address. I wanted to create a quick and simple user login using asp.net 4.0 membership
and email as the username, but it's getting to look nearly impossible.
On my CreateUserWizard control I have RequireEmail = false. I removed the EmailRequired validator and email textboxes and changed the User Name text to "User Name (email)" so that on the Register.aspx page I simply have the username and password/confirm
password textboxes. However, when I try to register a new account, I'm still getting "Not a valid e-mail". I have no idea where this message is coming from as it's not anywhere in my project.
Please tell me that there is a simple way to use the email as the username! Without that, this whole Membership system is silly.
Could you please post your applicable code. It looks like you are approaching this in correctly, but separately it sounds like you are using a regular expression validator, and that is what is returning your page not valid.
Doers get what they want! Everyone else gets what they get.
Without that, this whole Membership system is silly.
I
agree the membership system is silly, but for more reasons that just the one you cite.
As an alternative I've a starter/sample project that shows how to manage credentials without membership. Also, it's claims-aware. And one of the features it to allow email as the username which is the unique identifier for users.
Here's my aspx page followed by the code behind. It's very simple - there are three textboxes for user name/email, password and confirm. I enter an email for the user name, a password and confirm, and it tells me "Please enter a valid e-mail address."
Thanks for any insights!
<%@ Page Title="Register" Language="C#" MasterPageFile="~/Kim.master" AutoEventWireup="true"
CodeFile="~/Account/Register.aspx.cs" Inherits="Kimberly.Account.Register" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="cphKim">
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false"
OnCreatedUser="RegisterUser_CreatedUser"
RequireEmail="false"
DisableCreatedUser="true"
CompleteSuccessText="Your account has been created, but before you can login you must first verify your email address. A message has been sent to the email address you specified. Please check your email inbox and follow the instructions in that email to verify your account"
OnSendingMail="RegisterUser_SendingMail">
<LayoutTemplate>
<asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder>
<asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<WizardSteps>
<asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
<ContentTemplate>
<h2>
Create a New Account
</h2>
<p>
Use the form below to create a new account.
</p>
<p>
Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length.
</p>
<div class="accountInfo">
<fieldset class="register">
<legend>Account Information</legend>
<div style="width: 100%;">
<div class="accountFields">
<div class="accountFieldsLabel2">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</div>
<div class="accountFieldsText">
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="100%"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</div>
<div class="accountFields">
<div class="accountFieldsLabel2">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
</div>
<div class="accountFieldsText">
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password" Width="100%"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</div>
<div class="accountFields">
<div class="accountFieldsLabel2">
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
</div>
<div class="accountFieldsText">
<asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password" Width="100%"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server"
ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator>
</div>
</div>
</fieldset>
</div>
<div style="width: 50%; margin-left: auto; margin-right: auto; text-align: right;">
<asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" ValidationGroup="RegisterUserValidationGroup"/>
</div>
<asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="RegisterUserValidationGroup"/>
<span class="failureNotification">
<asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
</span>
</ContentTemplate>
<CustomNavigationTemplate>
</CustomNavigationTemplate>
</asp:CreateUserWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</asp:Content>
And here's the code behind:
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 System.Web.UI.HtmlControls;
using System.Linq.Expressions;
namespace Kimberly.Account
{
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
((Label)this.FindControlRecursive("UserNameLabel")).Text = "User Name (email):";
}
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (String.IsNullOrEmpty(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
protected void RegisterUser_SendingMail(object sender, MailMessageEventArgs e)
{
MembershipUser userInfo = Membership.GetUser(RegisterUser.UserName);
}
}
}
I don't know, something tells me I'm going about this all wrong. I'm trying to find tutorials on how to do this but no two are the same, and even simple things like "DisplayCancelButton=true" don't work - no Cancel button shows up. I also added the OnSendingEmail
event, but the code never gets called. I downloaded some tutorials from Scott Mitchell, but they don't work either.
I'm beginning to think you have to use Membership exactly as it's given to you, or it will never work.
In an old website I did all the membership stuff myself, including verification emails and my own shopping cart. However, I didn't want to go through all that again and was looking for something simpler and built-in. Apparently, there isn't anything.
In an old website I did all the membership stuff myself, including verification emails and my own shopping cart. However, I didn't want to go through all that again and was looking for something simpler and built-in. Apparently, there isn't anything.
Again, check the link I gave you above for the sample project. I think it's a decent starting point for managing user identity.
Thanks, checking it out - out of utter frustration with the Membership code. I finally got the register user to work with the email, but then the login stopped working! ValidateUser always returns false, despite all the hash algorithms I've tried.
Then I realized that even if I got login to work, the CreateUserWizard stuff still doesn't work!
Looking at the code bleow, DisableCreatedUser doesn't do anything, DisplayCancelButton does not display a Cancel button, CompleteSuccessText never gets displayed, and the SendingMail routine never fires. Microsoft has a LONG way to go to get Membership
to do anything!
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false"
OnCreatedUser="RegisterUser_CreatedUser"
RequireEmail="false"
DisableCreatedUser="true"
DisplayCancelButton="true"
CancelDestinationPageUrl="~/Default.aspx"
ContinueDestinationPageUrl="~/Default.aspx"
CompleteSuccessText="Your account has been created, but before you can login you must first verify your email address. A message has been sent to the email address you specified. Please check your email inbox and follow the instructions in that email to verify your account"
OnSendingMail="RegisterUser_SendingMail">
I only took a quick look BrockAllen, but that looks like another huge learning curve. I'd probably have to rewrite my entire application to fit around what you have, or modify yours to fit my app. Sigh...
lianaent
Member
76 Points
98 Posts
Membership with email as username
Dec 23, 2012 03:40 PM|LINK
I'm sure this is an old topic, but I'm coming up blank. It seems archaic to me that any site would still try to use unique usernames as credentials rather than an email address. I wanted to create a quick and simple user login using asp.net 4.0 membership and email as the username, but it's getting to look nearly impossible.
On my CreateUserWizard control I have RequireEmail = false. I removed the EmailRequired validator and email textboxes and changed the User Name text to "User Name (email)" so that on the Register.aspx page I simply have the username and password/confirm password textboxes. However, when I try to register a new account, I'm still getting "Not a valid e-mail". I have no idea where this message is coming from as it's not anywhere in my project.
Please tell me that there is a simple way to use the email as the username! Without that, this whole Membership system is silly.
Thanks!
Larry
remojr76
Participant
902 Points
303 Posts
Re: Membership with email as username
Dec 23, 2012 05:10 PM|LINK
Could you please post your applicable code. It looks like you are approaching this in correctly, but separately it sounds like you are using a regular expression validator, and that is what is returning your page not valid.
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: Membership with email as username
Dec 23, 2012 07:33 PM|LINK
I agree the membership system is silly, but for more reasons that just the one you cite.
As an alternative I've a starter/sample project that shows how to manage credentials without membership. Also, it's claims-aware. And one of the features it to allow email as the username which is the unique identifier for users.
https://github.com/brockallen/BrockAllen.MembershipReboot
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 01:09 AM|LINK
Here's my aspx page followed by the code behind. It's very simple - there are three textboxes for user name/email, password and confirm. I enter an email for the user name, a password and confirm, and it tells me "Please enter a valid e-mail address."
Thanks for any insights!
<%@ Page Title="Register" Language="C#" MasterPageFile="~/Kim.master" AutoEventWireup="true" CodeFile="~/Account/Register.aspx.cs" Inherits="Kimberly.Account.Register" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="cphKim"> <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser" RequireEmail="false" DisableCreatedUser="true" CompleteSuccessText="Your account has been created, but before you can login you must first verify your email address. A message has been sent to the email address you specified. Please check your email inbox and follow the instructions in that email to verify your account" OnSendingMail="RegisterUser_SendingMail"> <LayoutTemplate> <asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder> <asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder> </LayoutTemplate> <WizardSteps> <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server"> <ContentTemplate> <h2> Create a New Account </h2> <p> Use the form below to create a new account. </p> <p> Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length. </p> <div class="accountInfo"> <fieldset class="register"> <legend>Account Information</legend> <div style="width: 100%;"> <div class="accountFields"> <div class="accountFieldsLabel2"> <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label> </div> <div class="accountFieldsText"> <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="100%"></asp:TextBox> </div> <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </div> <div class="accountFields"> <div class="accountFieldsLabel2"> <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> </div> <div class="accountFieldsText"> <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password" Width="100%"></asp:TextBox> </div> <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </div> <div class="accountFields"> <div class="accountFieldsLabel2"> <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label> </div> <div class="accountFieldsText"> <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password" Width="100%"></asp:TextBox> </div> <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator> </div> </div> </fieldset> </div> <div style="width: 50%; margin-left: auto; margin-right: auto; text-align: right;"> <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" ValidationGroup="RegisterUserValidationGroup"/> </div> <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" ValidationGroup="RegisterUserValidationGroup"/> <span class="failureNotification"> <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal> </span> </ContentTemplate> <CustomNavigationTemplate> </CustomNavigationTemplate> </asp:CreateUserWizardStep> </WizardSteps> </asp:CreateUserWizard> </asp:Content>And here's the code behind:
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 System.Web.UI.HtmlControls; using System.Linq.Expressions; namespace Kimberly.Account { public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"]; ((Label)this.FindControlRecursive("UserNameLabel")).Text = "User Name (email):"; } protected void RegisterUser_CreatedUser(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */); string continueUrl = RegisterUser.ContinueDestinationPageUrl; if (String.IsNullOrEmpty(continueUrl)) { continueUrl = "~/"; } Response.Redirect(continueUrl); } protected void RegisterUser_SendingMail(object sender, MailMessageEventArgs e) { MembershipUser userInfo = Membership.GetUser(RegisterUser.UserName); } } }lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 01:45 AM|LINK
Well, I found one possible work-around:
protected void CreateUserButton_Click(object sender, EventArgs e) { RegisterUser.Email = RegisterUser.UserName; }lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 03:26 PM|LINK
I don't know, something tells me I'm going about this all wrong. I'm trying to find tutorials on how to do this but no two are the same, and even simple things like "DisplayCancelButton=true" don't work - no Cancel button shows up. I also added the OnSendingEmail event, but the code never gets called. I downloaded some tutorials from Scott Mitchell, but they don't work either.
I'm beginning to think you have to use Membership exactly as it's given to you, or it will never work.
In an old website I did all the membership stuff myself, including verification emails and my own shopping cart. However, I didn't want to go through all that again and was looking for something simpler and built-in. Apparently, there isn't anything.
lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 03:27 PM|LINK
You're probably right - Membership just doesn't work.
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: Membership with email as username
Dec 24, 2012 03:54 PM|LINK
Again, check the link I gave you above for the sample project. I think it's a decent starting point for managing user identity.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 06:34 PM|LINK
Thanks, checking it out - out of utter frustration with the Membership code. I finally got the register user to work with the email, but then the login stopped working! ValidateUser always returns false, despite all the hash algorithms I've tried.
Then I realized that even if I got login to work, the CreateUserWizard stuff still doesn't work!
Looking at the code bleow, DisableCreatedUser doesn't do anything, DisplayCancelButton does not display a Cancel button, CompleteSuccessText never gets displayed, and the SendingMail routine never fires. Microsoft has a LONG way to go to get Membership to do anything!
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser" RequireEmail="false" DisableCreatedUser="true" DisplayCancelButton="true" CancelDestinationPageUrl="~/Default.aspx" ContinueDestinationPageUrl="~/Default.aspx" CompleteSuccessText="Your account has been created, but before you can login you must first verify your email address. A message has been sent to the email address you specified. Please check your email inbox and follow the instructions in that email to verify your account" OnSendingMail="RegisterUser_SendingMail">lianaent
Member
76 Points
98 Posts
Re: Membership with email as username
Dec 24, 2012 07:20 PM|LINK
I only took a quick look BrockAllen, but that looks like another huge learning curve. I'd probably have to rewrite my entire application to fit around what you have, or modify yours to fit my app. Sigh...