Login to DNN wth Username or Email
Sep
26
Written by:
9/26/2009 8:57 AM
Logging to a DotNetNuke (DNN) site with either username or email is not supported out of the box. Here is a simple way to do this. First, make sure the membership provider in your Web.config is set to require unique emails when creating accounts. You'll find this on this section of your Web.config:
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="SiteSqlServer" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0" requiresUniqueEmail="true" passwordFormat="Encrypted" applicationName="DotNetNuke" description="Stores and retrieves membership data from the local Microsoft SQL Server database" />
Next, locate the following file: Website\DesktopModules\AuthenticationServices\DNN\Login.ascx.vb and replace the the cmdLogin_Click event handler with the following (see below how to change the caption for the username to something like 'Username or Email'):
Private Sub cmdLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdLogin.Click
If (UseCaptcha And ctlCaptcha.IsValid) OrElse (Not UseCaptcha) Then
Dim loginStatus As UserLoginStatus = UserLoginStatus.LOGIN_FAILURE
Dim objUser As Entities.Users.UserInfo = Nothing
Dim authenticated As Boolean = Null.NullBoolean
Dim message As String = Null.NullString
Dim objUsers As ArrayList = Nothing
Dim strUsername As String = txtUsername.Text
Dim intTotalRecords As Integer = -1
If DotNetNuke.Services.Mail.Mail.IsValidEmailAddress(strUsername, PortalId) _
And MembershipProvider.Instance.RequiresUniqueEmail Then
objUsers = DotNetNuke.Entities.Users.UserController.GetUsersByEmail(PortalId, strUsername, 0, 1, intTotalRecords)
If intTotalRecords > 0 Then
objUser = objUsers(0)
strUsername = objUser.Username
Else
strUsername = txtUsername.Text
End If
End If
objUser = Entities.Users.UserController.ValidateUser(PortalId, strUsername, txtPassword.Text, "DNN", txtVerification.Text, PortalSettings.PortalName, IPAddress, loginStatus)
If loginStatus = UserLoginStatus.LOGIN_USERNOTAPPROVED Then
'Check if its the first time logging in to a verified site
If PortalSettings.UserRegistration = PortalRegistrationType.VerifiedRegistration Then
If Not rowVerification1.Visible Then
'Display Verification Rows so User can enter verification code
rowVerification1.Visible = True
rowVerification2.Visible = True
message = "EnterCode"
Else
If txtVerification.Text <> "" Then
message = "InvalidCode"
Else
message = "EnterCode"
End If
End If
Else
message = "UserNotAuthorized"
End If
Else
authenticated = (loginStatus <> UserLoginStatus.LOGIN_FAILURE)
End If
'Raise UserAuthenticated Event
Dim eventArgs As UserAuthenticatedEventArgs = New UserAuthenticatedEventArgs(objUser, txtUsername.Text, loginStatus, "DNN")
eventArgs.Authenticated = authenticated
eventArgs.Message = message
OnUserAuthenticated(eventArgs)
End If
End Sub
NOTE: Once setup you can use DNN language settings to change the caption of the username field to say 'Username or Email' by going to 'Admin' > 'Languages' > 'Shared Resources', locate the 'Username.Text' entry, modify to say something like 'Username or Email' and save.
2 comment(s) so far...
Re: Login to DNN wth Username or Email
This is just what I've been looking, but have come across a problem. Does this only work with certain server configs? I've made the changes above and am getting the error:
A critical error has occurred. C:\\DesktopModules\AuthenticationServices\DNN\Login.ascx.vb(171): error BC30456: 'IsValidEmailAddress' is not a member of 'DotNetNuke.Services.Mail.Mail'.
I'm running DNN 04.09.05 on .NET Framework 2.0.50727.832 on Windows 2003 Server (IIS6). Any ideas? I'd really like to implement this!
Thank :)
By Simon Holmes on
12/29/2009 1:29 AM
|
Re: Login to DNN wth Username or Email
You can try to replace using IsValidEmailAddress with a Regex version of the same as follows: Private Sub cmdLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdLogin.Click
If (UseCaptcha And ctlCaptcha.IsValid) OrElse (Not UseCaptcha) Then Dim loginStatus As UserLoginStatus = UserLoginStatus.LOGIN_FAILURE Dim objUser As Entities.Users.UserInfo = Nothing Dim authenticated As Boolean = Null.NullBoolean Dim message As String = Null.NullString Dim objUsers As ArrayList = Nothing Dim strUsername As String = txtUsername.Text Dim intTotalRecords As Integer = -1 Dim objRegex As Regex = New Regex( _ "\b[a-zA-Z0-9._%-+']+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b", _ RegexOptions.IgnoreCase _ Or RegexOptions.CultureInvariant _ Or RegexOptions.IgnorePatternWhitespace _ Or RegexOptions.Compiled _ )
If objRegex.IsMatch(strUsername) _ And MembershipProvider.Instance.RequiresUniqueEmail Then
objUsers = DotNetNuke.Entities.Users.UserController.GetUsersByEmail(PortalId, strUsername, 0, 1, intTotalRecords) If intTotalRecords > 0 Then objUser = objUsers(0) strUsername = objUser.Username Else strUsername = txtUsername.Text End If
End If
objUser = Entities.Users.UserController.ValidateUser(PortalId, strUsername, txtPassword.Text, "DNN", txtVerification.Text, PortalSettings.PortalName, IPAddress, loginStatus)
If loginStatus = UserLoginStatus.LOGIN_USERNOTAPPROVED Then 'Check if its the first time logging in to a verified site If PortalSettings.UserRegistration = PortalRegistrationType.VerifiedRegistration Then If Not rowVerification1.Visible Then 'Display Verification Rows so User can enter verification code rowVerification1.Visible = True rowVerification2.Visible = True message = "EnterCode" Else If txtVerification.Text "" Then message = "InvalidCode" Else message = "EnterCode" End If End If Else message = "UserNotAuthorized" End If Else authenticated = (loginStatus UserLoginStatus.LOGIN_FAILURE) End If
'Raise UserAuthenticated Event Dim eventArgs As UserAuthenticatedEventArgs = New UserAuthenticatedEventArgs(objUser, txtUsername.Text, loginStatus, "DNN") eventArgs.Authenticated = authenticated eventArgs.Message = message OnUserAuthenticated(eventArgs) End If
End Sub
By Charlie Marval on
12/29/2009 2:56 AM
|