Code changes needed to update for 4.6.2 (should apply to 4.7.0 also):
ADSI Changes
----------------------------------------------------
Settings.aspx
- Add checkbox to allow setting domain trimming
<tr>
<td class="SubHead" width="200"><dnn:label id="plStripDomainName" runat="server" controlname="chkStripDomainName" text="Strip Domain?" /></td>
<td valign="top"><asp:CheckBox ID="chkStripDomainName" runat="server" CssClass="NormalTextBox" /></td>
</tr>
Settings.aspx.vb
- Change Update Config in UpdateSettings to include Strip Domain value
Public Overrides Sub UpdateSettings()
...
Configuration.UpdateConfig(_portalSettings.PortalId, Me.chkAuthentication.Checked, Me.txtRootDomain.Text, Me.txtEmailDomain.Text, Me.txtUserName.Text, Me.txtPassword.Text, Me.chkSynchronizeRole.Checked, Me.chkSynchronizePassword.Checked, Me.chkStripDomainName.Checked, providerTypeName, authenticationType)
...
End Sub
- Add code to handle chkStripDomainName
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
...
chkAuthentication.Checked = config.WindowsAuthentication
chkSynchronizeRole.Checked = config.SynchronizeRole
chkSynchronizePassword.Checked = config.SynchronizePassword
chkStripDomainName.Checked = config.StripDomainName
txtRootDomain.Text = config.RootDomain
txtUserName.Text = config.UserName
txtEmailDomain.Text = config.EmailDomain
...
End Sub
AuthenticationController.vb
- Check setting and apply changes to UserName; fix references in rest of Sub
- Also includes authentication redirect fix
Public Sub AuthenticationLogon()
Dim _config As Configuration = Configuration.GetConfig()
Dim objAuthUserController As New Authentication.ActiveDirectory.UserController
Dim authCookies As String = Configuration.AUTHENTICATION_KEY & "_" & _portalSettings.PortalId.ToString
Dim LoggedOnUserName As String = HttpContext.Current.Request.ServerVariables(Configuration.LOGON_USER_VARIABLE)
If LoggedOnUserName.Length > 0 Then
Dim UserName As String = LoggedOnUserName
If _config.StripDomainName Then
UserName = DotNetNuke.Authentication.ActiveDirectory.ADSI.Utilities.TrimUserDomainName(UserName)
End If
Dim objDNNUser As DotNetNuke.Entities.Users.UserInfo
Dim objAuthUser As Authentication.ActiveDirectory.UserInfo
Dim intUserId As Integer
objDNNUser = DotNetNuke.Entities.Users.UserController.GetUserByName(_portalSettings.PortalId, UserName, False)
If Not objDNNUser Is Nothing Then
intUserId = objDNNUser.UserID
' Synchronize role membership if it's required in settings
If _config.SynchronizeRole Then
SynchronizeRoles(UserName, intUserId)
End If
Else
' User not exists in DNN database, obtain user info from provider to add new
objAuthUser = objAuthUserController.GetUser(LoggedOnUserName)
objDNNUser = CType(objAuthUser, DotNetNuke.Entities.Users.UserInfo)
UserName = objDNNUser.Username
If _config.StripDomainName Then
UserName = DotNetNuke.Authentication.ActiveDirectory.ADSI.Utilities.TrimUserDomainName(UserName)
End If
objDNNUser.Username = UserName
If Not objAuthUser Is Nothing Then
Dim createStatus As UserCreateStatus = objAuthUserController.AddDNNUser(objAuthUser)
intUserId = objAuthUser.UserID
SetStatus(_portalSettings.PortalId, AuthenticationStatus.WinLogon)
End If
End If
If intUserId > 0 Then
FormsAuthentication.SetAuthCookie(Convert.ToString(UserName), True)
'check if user has supplied custom value for expiration
Dim PersistentCookieTimeout As Integer
If Not Config.GetSetting("PersistentCookieTimeout") Is Nothing Then
PersistentCookieTimeout = Integer.Parse(Config.GetSetting("PersistentCookieTimeout"))
'only use if non-zero, otherwise leave as asp.net value
If PersistentCookieTimeout <> 0 Then
'locate and update cookie
Dim authCookie As String = FormsAuthentication.FormsCookieName
For Each cookie As String In HttpContext.Current.Response.Cookies
If cookie.Equals(authCookie) Then
HttpContext.Current.Response.Cookies(cookie).Expires = DateTime.Now.AddMinutes(PersistentCookieTimeout)
End If
Next
End If
End If
SetStatus(_portalSettings.PortalId, AuthenticationStatus.WinLogon)
' Get ipAddress for eventLog
Dim ipAddress As String = ""
If Not HttpContext.Current.Request.UserHostAddress Is Nothing Then
ipAddress = HttpContext.Current.Request.UserHostAddress
End If
Dim objEventLog As New Services.Log.EventLog.EventLogController
Dim objEventLogInfo As New Services.Log.EventLog.LogInfo
objEventLogInfo.AddProperty("IP", ipAddress)
objEventLogInfo.LogPortalID = _portalSettings.PortalId
objEventLogInfo.LogPortalName = _portalSettings.PortalName
objEventLogInfo.LogUserID = intUserId
objEventLogInfo.LogUserName = UserName
objEventLogInfo.AddProperty("WindowsAuthentication", "True")
objEventLogInfo.LogTypeKey = "LOGIN_SUCCESS"
objEventLog.AddLog(objEventLogInfo)
End If
Else
' Not Windows Authentication
End If
' params "logon=windows" does nothing, just to force page to be refreshed
'Dim strURL As String = NavigateURL(_portalSettings.ActiveTab.TabID, "", "logon=windows")
'Updated to redirect to querrystring passed in prior to authentication
Dim querystringparams As String = "logon=" & DateTime.Now.Ticks.ToString()
Dim strURL As String = NavigateURL(_portalSettings.ActiveTab.TabID, String.Empty, querystringparams)
If Not HttpContext.Current.Request.Cookies("DNNReturnTo" + _portalSettings.PortalId.ToString()) Is Nothing Then
querystringparams = HttpContext.Current.Request.Cookies("DNNReturnTo" + _portalSettings.PortalId.ToString()).Value
If querystringparams <> String.Empty And querystringparams.IndexOf("WindowsSignin.aspx") < 0 Then strURL = querystringparams
End If
HttpContext.Current.Response.Redirect(strURL, True)
End Sub
Public Function ProcessFormAuthentication(ByVal LoggedOnUserName As String, ByVal LoggedOnPassword As String) As UserInfo 'DotNetNuke.Entities.Users.UserInfo
Dim _config As Configuration = Configuration.GetConfig()
Dim objAuthUserController As New UserController
Dim objUsers As New DotNetNuke.Entities.Users.UserController
If _config.WindowsAuthentication Then
Dim UserName As String = LoggedOnUserName
If _config.StripDomainName Then
UserName = DotNetNuke.Authentication.ActiveDirectory.ADSI.Utilities.TrimUserDomainName(UserName)
End If
Dim objAuthUser As UserInfo = objAuthUserController.GetUser(UserName, LoggedOnPassword)
Return objAuthUser
End If
Return Nothing
End Function
Public Function GetDNNUser(ByVal PortalID As Integer, ByVal LoggedOnUserName As String) As DotNetNuke.Entities.Users.UserInfo
Dim _config As Configuration = Configuration.GetConfig()
Dim objUser As DotNetNuke.Entities.Users.UserInfo
Dim UserName As String = LoggedOnUserName
If _config.StripDomainName Then
UserName = DotNetNuke.Authentication.ActiveDirectory.ADSI.Utilities.TrimUserDomainName(UserName)
End If
'TODO: Check the new concept of 3.0 for user in multi portal
' check if this user exists in database for any portal
objUser = DotNetNuke.Entities.Users.UserController.GetUserByName(Null.NullInteger, UserName, False)
If Not objUser Is Nothing Then
' Check if user exists in this portal
If DotNetNuke.Entities.Users.UserController.GetUserByName(PortalID, UserName, False) Is Nothing Then
' The user does not exist in this portal - add them
objUser.PortalID = PortalID
DotNetNuke.Entities.Users.UserController.CreateUser(objUser)
End If
Return objUser
Else
' the user does not exist
Return Nothing
End If
End Function
Configuration.vb
- Add code for StripDomainName
Public Class Configuration
...
Private mPortalId As Integer
Private mWindowsAuthentication As Boolean = False
Private mRootDomain As String = ""
Private mUserName As String = ""
Private mPassword As String = ""
Private mSynchronizeRole As Boolean = False
Private mSynchronizePassword As Boolean = False ' reserve for future feature
Private mStripDomainName As Boolean = False
Private mProviderTypeName As String = DefaultProviderTypeName
Private mAuthenticationType As String = DefaultAuthenticationType
Private mEmailDomain As String = DefaultEmailDomain
...
Sub New()
...
mWindowsAuthentication = CType(Null.GetNull(settings("AD_WindowsAuthentication"), mWindowsAuthentication), Boolean)
mSynchronizeRole = CType(Null.GetNull(settings("AD_SynchronizeRole"), mSynchronizeRole), Boolean)
mSynchronizePassword = CType(Null.GetNull(settings("AD_SynchronizePassword"), mSynchronizePassword), Boolean)
mStripDomainName = CType(Null.GetNull(settings("AD_StripDomainName"), mStripDomainName), Boolean)
mRootDomain = CType(Null.GetNull(settings("AD_RootDomain"), mRootDomain), String)
mEmailDomain = CType(Null.GetNull(settings("AD_EmailDomain"), mEmailDomain), String)
mUserName = CType(Null.GetNull(settings("AD_UserName"), mUserName), String)
mProviderTypeName = CType(Null.GetNull(settings("AD_ProviderTypeName"), mProviderTypeName), String)
mAuthenticationType = CType(Null.GetNull(settings("AD_AuthenticationType"), mAuthenticationType), String)
mPassword = objSecurity.Decrypt(AUTHENTICATION_KEY, CType(Null.GetNull(settings("AD_AuthenticationPassword"), mPassword.ToString), String))
...
End Sub
...
Public Shared Sub UpdateConfig(ByVal PortalID As Integer, _
ByVal WindowsAuthentication As Boolean, _
ByVal RootDomain As String, _
ByVal EmailDomain As String, _
ByVal AuthenticationUserName As String, _
ByVal AuthenticationPassword As String, _
ByVal SynchronizeRole As Boolean, _
ByVal SynchronizePassword As Boolean, _
ByVal StripDomainName As Boolean, _
ByVal ProviderTypeName As String, _
ByVal AuthenticationType As String)
...
objModules.UpdateModuleSetting(intModuleId, "AD_WindowsAuthentication", WindowsAuthentication.ToString)
objModules.UpdateModuleSetting(intModuleId, "AD_SynchronizeRole", SynchronizeRole.ToString)
objModules.UpdateModuleSetting(intModuleId, "AD_SynchronizePassword", SynchronizePassword.ToString)
objModules.UpdateModuleSetting(intModuleId, "AD_StripDomainName", StripDomainName.ToString)
objModules.UpdateModuleSetting(intModuleId, "AD_RootDomain", RootDomain)
objModules.UpdateModuleSetting(intModuleId, "AD_EmailDomain", EmailDomain)
objModules.UpdateModuleSetting(intModuleId, "AD_UserName", AuthenticationUserName)
objModules.UpdateModuleSetting(intModuleId, "AD_ProviderTypeName", ProviderTypeName)
objModules.UpdateModuleSetting(intModuleId, "AD_AuthenticationType", AuthenticationType)
...
End Sub
-- Add New Property
''' -------------------------------------------------------------------
''' <summary>
''' Determin if Domain Name should be stripped from UserName
''' </summary>
''' <remarks>
''' In an environment with single domain or unique username across domains
''' </remarks>
''' <history>
''' [jhoskins] 10/10/2007 Created
''' </history>
''' -------------------------------------------------------------------
Public ReadOnly Property StripDomainName() As Boolean
Get
Return mStripDomainName
End Get
End Property
UserController.vb
-- Set authentication type so that we can get to it later
Public Function AddDNNUser(ByVal AuthenticationUser As UserInfo) As UserCreateStatus
Dim _portalSettings As PortalSettings = PortalController.GetCurrentPortalSettings
Dim objSecurity As New PortalSecurity
DotNetNuke.Services.Authentication.AuthenticationController.SetAuthenticationType("ActiveDirectory")
...
End Function
AuthenticationModule.vb
- Authentication redirect fix
Public Sub OnAuthenticateRequest(ByVal s As Object, ByVal e As EventArgs)
...
If config.WindowsAuthentication Then
Dim authStatus As AuthenticationStatus = AuthenticationController.GetStatus(_portalSettings.PortalId)
Dim blnWinLogon As Boolean = (Request.RawUrl.ToLower.IndexOf((Configuration.AUTHENTICATION_LOGON_PAGE).ToLower) > -1)
Dim blnWinLogoff As Boolean = (authStatus = AuthenticationStatus.WinLogon) _
AndAlso (Request.RawUrl.ToLower.IndexOf((Configuration.AUTHENTICATION_LOGOFF_PAGE).ToLower) > -1)
If (authStatus = AuthenticationStatus.Undefined) Then 'OrElse (blnWinLogon) Then
AuthenticationController.SetStatus(_portalSettings.PortalId, AuthenticationStatus.WinProcess)
Dim url As String
If Request.ApplicationPath = "/" Then
url = Configuration.AUTHENTICATION_PATH & Configuration.AUTHENTICATION_LOGON_PAGE & "?tabid=" & _portalSettings.ActiveTab.TabID.ToString
Else
url = Request.ApplicationPath & Configuration.AUTHENTICATION_PATH & Configuration.AUTHENTICATION_LOGON_PAGE & "?tabid=" & _portalSettings.ActiveTab.TabID.ToString
End If
Try
Dim refUrl As String = Request.RawUrl
Response.Clear()
Response.Cookies("DNNReturnTo" & _portalSettings.PortalId.ToString()).Value = refUrl
Response.Cookies("DNNReturnTo" & _portalSettings.PortalId.ToString()).Path = "/"
Response.Cookies("DNNReturnTo" & _portalSettings.PortalId.ToString()).Expires = DateTime.Now.AddMinutes(2)
Catch
End Try
Response.Redirect(url)
ElseIf (Not authStatus = AuthenticationStatus.WinLogoff) AndAlso blnWinLogoff Then
Dim objAuthentication As New AuthenticationController
objAuthentication.AuthenticationLogoff()
ElseIf (authStatus = AuthenticationStatus.WinLogoff) AndAlso blnWinLogon Then ' has been logoff before
AuthenticationController.SetStatus(_portalSettings.PortalId, AuthenticationStatus.Undefined)
Response.Redirect(Request.RawUrl)
End If
End If
...
End Sub
Core Changes
----------------------------------------------------
AspNetMembershipProvider.vb
-- Checks for provider type. If AD, ignores password in AD - this will also fix multiple sites with AD user issues.
Public Overrides Function CreateUser(ByRef user As UserInfo) As UserCreateStatus
Dim createStatus As UserCreateStatus
Dim authType As DotNetNuke.Services.Authentication.AuthenticationInfo = DotNetNuke.Services.Authentication.AuthenticationController.GetAuthenticationType()
Dim isADSIProvider As Boolean = False
Dim ProviderTypeName As String = String.Empty
If Not authType Is Nothing Then ProviderTypeName = authType.AuthenticationType
isADSIProvider = (ProviderTypeName.IndexOf("ActiveDirectory") >= 0)
...
Else
' the username exists so we should now verify the password
If isADSIProvider Or ValidateUser(objVerifyUser.PortalID, user.Username, user.Membership.Password) Then
...
End Function