Page view counter

Using Email as UserName

Rate It (2)

Last post 05-18-2009 3:48 PM by relish27. 45 replies.

Sort Posts:

  • Using Email as UserName

    08-16-2005, 7:09 AM
    • Loading...
    • paul1664
    • Joined on 07-08-2003, 1:28 PM
    • Posts 33
    • Points 170
    Hi,

    I spotted some previous posts about using the users Email address as UserName.

    http://forums.asp.net/996898/ShowPost.aspx

    http://forums.asp.net/597391/ShowPost.aspx

    Is there a 'better' or 'proper' way of doing this in beta 2 or the final release?

    Thanks, Paul.

  • Re: Using Email as UserName

    08-17-2005, 6:31 AM
    • Loading...
    • Edgardo
    • Joined on 04-28-2005, 4:21 PM
    • Buenos Aires, Argentina
    • Posts 153
    • Points 765
    Well if you really want your users to login with both the username and email address you need to implement your own MP or find a way to extend it.

    I'm wondering if its possible to inherit from SqlMembershipProvider so you don't have to reimplement every method, only override ValidateUser()
  • Re: Using Email as UserName

    08-18-2005, 12:54 PM
    • Loading...
    • farmas
    • Joined on 08-05-2002, 8:19 AM
    • Redmond, WA
    • Posts 228
    • Points 968
    • AspNetTeam
    It is definetly possible to inherit from the SqlMembershipProvider class and just override the methods you want/need
  • Re: Using Email as UserName

    08-21-2005, 1:36 AM
    • Loading...
    • Scott W
    • Joined on 04-13-2003, 12:50 AM
    • Earth
    • Posts 130
    • Points 650
    I have the email address as the username and there's no need to override or extend the membership provider at all. All you are doing is using the email in both the email fields and the username fields.

    For the CreateUserWizard control, you'll need to template it and label the username textbox as email address.

    Then set an event handler for the CreatingUser event:

    public void RegisterNewUser_CreatingUser(object sender, System.EventArgs e)
    {
         CreateUserWizard cuw = (CreateUserWizard)sender;
         cuw.Email = cuw.UserName;
    }

    Then template your login control and set the label on the username textbox to email address, and put in a regex validation control to make sure they enter a valid email address.

    You might have to set the membership provider properties in web.config to NOT require unique email addresses, but it won't matter as it will require a unique username anyway.


    It's that simple.

    Scott Willsey
  • Re: Using Email as UserName

    12-08-2005, 8:51 AM
    • Loading...
    • gb@e-works
    • Joined on 12-06-2005, 1:43 PM
    • Posts 6
    • Points 30
    Hi,

    I'm kinda new to ASP.NET and started working with DotNetNuke. Is it possible to use the e-mail as username in DotNetNuke registration?

    I don't have a clue about databases, but I'm willing to learn. :)

    Regards,
    Georg

  • Re: Using Email as UserName

    01-30-2006, 2:51 PM
    • Loading...
    • radmanmm
    • Joined on 09-25-2002, 9:33 AM
    • Posts 62
    • Points 178

    I believe the creating a customer provider is probably the proper way to do this, however I did not want to have to override every method in the provider to get the functionality I needed to use the email address as the username. I found no explainations on the net to date that would describe exactly which methods I would need to override to get the email to be used as the username.  Here are the issues I ran into and my solution that looks like it will work.

    The username cannot be changed easily, I originally thought I could just set the username equal to the email address.  The problem I ran into is, the user might need to change his/her email address. If we did not change the username but only the email address, the user would see the original email address everytime the username property is referenced.

    I thought I could then change the username by updating fields in the users table.  This proved to be a problem because other entities like the personalization engine needed to put user entries in.  I ran into syncronization issues where I made the change but the personalization engine was still using the previous username to store information.

    So I decided the best course of action (that I could find anyway) was to generate a unique username and hide it from the user.  Require email addresses to be unique and hide the username entry from the create user control.  This fixed 90% of my problem. I simply checked for the User.Identity.Username for an email address and if I found one I would call the GetUserNameByEmail method to get the username for loading the MembershipUser object.  However there was still one little minor problem "Password Recovery". I tried to extend the PasswordRecovery control but found that there were not enough events in the right places to handle the email/username swap.

    In the end I wrote allot of username/email toggling code into the various event of the PasswordRecovery control to handle the last 10% I was missing from my solution. I use a session variable and the Page_Load, VerifyingUser, SendingMail, and PreRender events to accomplish the task.

    This solution does not seem very elegant solution to me, but there is not very much information on the subject and I needed to get the job done.

    If anyone has any better solution with a sample or solid explanation I would love to hear it. If anyone is interested in the code I would be happy to provide it.

    Thanks,

  • Re: Using Email as UserName

    01-30-2006, 6:50 PM
    • Loading...
    • Edgardo
    • Joined on 04-28-2005, 4:21 PM
    • Buenos Aires, Argentina
    • Posts 153
    • Points 765
    The best is to leave the default provider as-is and::

    1) Set the provider to require a unique email
    2) Prevent users from using an email address as their UserNames

    Then, on the login control you handle the LogginIn event, via Regex, you see if what the user input as UserName is an email or not, if thats the case then you find the UserName for that email and make the Login control validate that credentials.

    I posted an example on my blog:
    http://edgardorossetto.net/2006/01/autenticacin-por-username-o-email.html

    Its in spanish, though, but the C# code should be easy to understand.

    Hope it helps
  • Re: Using Email as UserName

    01-30-2006, 8:46 PM
    • Loading...
    • radmanmm
    • Joined on 09-25-2002, 9:33 AM
    • Posts 62
    • Points 178
    So how does this work if the user needs to change the email address?  If the username isn't changed then every time they click on forgot password it will show the original email that was used to create the account and not the one that they changed it to.  Also when you look at the user through the administration page you would have to know the users original email to know which user entry to edit.
  • Re: Using Email as UserName

    01-31-2006, 2:17 AM
    • Loading...
    • Edgardo
    • Joined on 04-28-2005, 4:21 PM
    • Buenos Aires, Argentina
    • Posts 153
    • Points 765
    No, in the database the fields remain exactly the same (username is username and email is email), with my example it only changes how the user can log in (with their username or email), but the rest behaves exactly the same.

  • Re: Using Email as UserName

    01-31-2006, 4:37 AM
    • Loading...
    • Scott W
    • Joined on 04-13-2003, 12:50 AM
    • Earth
    • Posts 130
    • Points 650
    The only benefit I see to that over just setting the username to the email address is for if you want the user to be able to log into a forum or something using a forum username - although you could still just have a profile property for showing on the forum and have them use their email address and password to login with.
    Scott Willsey
  • Re: Using Email as UserName

    01-31-2006, 9:26 AM
    • Loading...
    • radmanmm
    • Joined on 09-25-2002, 9:33 AM
    • Posts 62
    • Points 178
    I understand that part of it Edgardo, what I am asking is what is the username actually set to? Email address?  If this is the case then what happens when the user needs to change the email because it is no longer valid?  What happens to the UserName field?  What happens after the user has changed the email and then forgets the password?
  • Re: Using Email as UserName

    01-31-2006, 9:29 AM
    • Loading...
    • radmanmm
    • Joined on 09-25-2002, 9:33 AM
    • Posts 62
    • Points 178
    I actually do use profile fields to display first and last name as the login status instead of the username.  But what I am saying is that you can't just set the username equal to the email because there are other administrative functions that use the username, like password recovery.  And the username cannot be changed easily, but the email address might need to change.
  • Re: Using Email as UserName

    01-31-2006, 10:12 AM
    • Loading...
    • Edgardo
    • Joined on 04-28-2005, 4:21 PM
    • Buenos Aires, Argentina
    • Posts 153
    • Points 765
    radmanmm wrote:
    I understand that part of it Edgardo, what I am asking is what is the username actually set to? Email address?  If this is the case then what happens when the user needs to change the email because it is no longer valid?  What happens to the UserName field?  What happens after the user has changed the email and then forgets the password?


    The username (in the db) is set to whatever username the user chose when he/she registered, like I said before everything works normally (default provider behavior), the login trick is only to allow the user to login using his email or his username.
  • Re: Using Email as UserName

    01-31-2006, 1:53 PM
    • Loading...
    • radmanmm
    • Joined on 09-25-2002, 9:33 AM
    • Posts 62
    • Points 178

    I see what you are saying. The solution I was describing however is when you want the email to be the only means of logging in.  I don't want the user having to guess at selecting a unique login name.  If it is in use then they would be asked repeatedly for a unique username.  This just makes the process easier for the user because they typically can remember "user@isp.com" instead of "user1923".

    I posted to this thread so that people searching for a similar solution could find one.  I spent many hours trying to find an answer on the web and was unsuccessful. So when I found a solution I wanted to provide it for the next guy.

  • Re: Using Email as UserName

    01-31-2006, 4:00 PM
    • Loading...
    • Scott W
    • Joined on 04-13-2003, 12:50 AM
    • Earth
    • Posts 130
    • Points 650

    radmanmm wrote:
    I actually do use profile fields to display first and last name as the login status instead of the username.  But what I am saying is that you can't just set the username equal to the email because there are other administrative functions that use the username, like password recovery.  And the username cannot be changed easily, but the email address might need to change.

    Well, I'm telling you I do use the email address as the username in asp.net 2.0 and I can use all the administrative functions and there's no issue. So if you want to say it can't be done, that's great... but it's not true.

    It sounds like you want to do exactly what I'm doing (have the login only using email address and password, and having the username = email address in the db) only you don't want to believe that I'm doing it. If you don't want to listen to me saying it can be done, then wait a bit and I'll sell you my app when I release it and then you can do it too.  Big Smile [:D]  Or you can read my first post in this thread and make your own app do what you are trying to do.

    Scott Willsey
Page 1 of 4 (46 items) 1 2 3 4 Next >