views:

1043

answers:

1

In the context of a DNN module, what's a good generic way to find out what the URL to the login functionality is?

+2  A: 

Here is a utility method that gets the login URL:

C#

        /// <summary>
        /// Gets the login URL for the given portal from the current <paramref name="request"/>.
        /// </summary>
        /// <param name="portalSettings">The portal settings.</param>
        /// <param name="request">The request.</param>
        /// <returns>The URL for the login page</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="portalSettings"/> or <paramref name="request"/> is null.</exception>
        public static string GetLoginUrl(PortalSettings portalSettings, HttpRequest request)
        {
            if (portalSettings != null && request != null)
            {
                int tabId = portalSettings.ActiveTab.TabID;
                string controlKey = "Login";
                string returnUrl = request.RawUrl;
                if (returnUrl.IndexOf("?returnurl=", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?returnurl=", StringComparison.OrdinalIgnoreCase));
                }

                returnUrl = HttpUtility.UrlEncode(returnUrl);

                if (!Null.IsNull(portalSettings.LoginTabId) && string.IsNullOrEmpty(request.QueryString["override"]))
                {
                    // user defined tab
                    controlKey = string.Empty;
                    tabId = portalSettings.LoginTabId;
                }
                else if (!Null.IsNull(portalSettings.HomeTabId))
                {
                    // portal tab
                    tabId = portalSettings.HomeTabId;
                }

                // else current tab
                return Globals.NavigateURL(tabId, controlKey, new string[] { "returnUrl=" + returnUrl });
            }

            throw new ArgumentNullException(portalSettings == null ? "portalSettings" : "request");
        }

VB.NET

''' <summary>
''' Gets the login URL for the given portal from the current <paramref name="request"/>.
''' </summary>
''' <param name="portalSettings">The portal settings.</param>
''' <param name="request">The request.</param>
''' <returns>The URL for the login page</returns>
''' <exception cref="ArgumentNullException">if <paramref name="portalSettings"/> or <paramref name="request"/> is null.</exception>
Public Shared Function GetLoginUrl(portalSettings As PortalSettings, request As HttpRequest) As String
    If portalSettings <> Nothing AndAlso request <> Nothing Then
     Dim tabId As Integer = portalSettings.ActiveTab.TabID
     Dim controlKey As String = "Login"
     Dim returnUrl As String = request.RawUrl
     If returnUrl.IndexOf("?returnurl=", StringComparison.OrdinalIgnoreCase) > -1 Then
      returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?returnurl=", StringComparison.OrdinalIgnoreCase))
     End If

     returnUrl = HttpUtility.UrlEncode(returnUrl)

     If Not Null.IsNull(portalSettings.LoginTabId) AndAlso String.IsNullOrEmpty(request.QueryString("override")) Then
      ' user defined tab
      controlKey = String.Empty
      tabId = portalSettings.LoginTabId
     ElseIf Not Null.IsNull(portalSettings.HomeTabId) Then
      ' portal tab
      tabId = portalSettings.HomeTabId
     End If

     ' else current tab
     Return Globals.NavigateURL(tabId, controlKey, New String() {"returnUrl=" + returnUrl})
    End If

    Throw New ArgumentNullException(If(portalSettings = Nothing, "portalSettings", "request"))
End Function
Ian Robinson