locked
Membership.GetUser() is really slow RRS feed

  • Question

  • User-72859878 posted

    I wrote a asp.net webforms application a few years ago where I created my own membership provider so I could authenticate my users against AD.  I have a method in the application which calls  Membership.GetUser();.  Over the last few months this step has gone from taking a couple seconds to 18 seconds.  My code surounding the membership provider hasn't changed in 2 years so I am confused why this slowdown is occuring.

    Any Suggestions?

    Thursday, January 7, 2016 10:25 PM

Answers

  • User614698185 posted

    Hi donndela,

    The GetUser() have many calls because actually need more information's to give, like below:

    public static MembershipUser GetUser()
    {
        return GetUser(GetCurrentUserName(), true);
    }
    
    private static string GetCurrentUserName()
    {
        if (HostingEnvironment.IsHosted)
        {
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                return current.User.Identity.Name;
            }
        }
        IPrincipal currentPrincipal = Thread.CurrentPrincipal;
        if ((currentPrincipal != null) && (currentPrincipal.Identity != null))
        {
            return currentPrincipal.Identity.Name;
        }
        return string.Empty;
    }
    
    public static MembershipUser GetUser(string username, bool userIsOnline)
    {
        SecUtility.CheckParameter(ref username, true, false, true, 0, "username");
        return Provider.GetUser(username, userIsOnline);
    }
    
    internal static void CheckParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName)
    {
        if (param == null)
        {
            if (checkForNull)
            {
                throw new ArgumentNullException(paramName);
            }
        }
        else
        {
            param = param.Trim();
            if (checkIfEmpty && (param.Length < 1))
            {
                throw new ArgumentException(SR.GetString("Parameter_can_not_be_empty", new object[] { paramName }), paramName);
            }
            if ((maxSize > 0) && (param.Length > maxSize))
            {
                throw new ArgumentException(SR.GetString("Parameter_too_long", new object[] { paramName, maxSize.ToString(CultureInfo.InvariantCulture) }), paramName);
            }
            if (checkForCommas && param.Contains(","))
            {
                throw new ArgumentException(SR.GetString("Parameter_can_not_contain_comma", new object[] { paramName }), paramName);
            }
        }
    }
    

    For more information, please see: http://stackoverflow.com/questions/13490708/asp-net-custom-membership-provider-many-calls-to-getuser-make-the-application

    http://stackoverflow.com/questions/11333537/request-isauthenticated-vs-membership-getuser

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2016 6:24 AM