Search code examples
asp.netasp.net-membershipmembership-provider

When ApplicationID is generated in Membership.CreateUser method?


I was working with ASP.Net Membership and was wondering when exactly ApplicationID is generated. I want to know what events are called and when does ApplicationID is generated when we use "Membership.CreateUser" method.

I googled it for some time but couldn't find any satisfactory answer.

Can anyone explain what is the working of this method?

Thanks


Solution

  • I want to know what events are called and when does ApplicationID is generated when we use "Membership.CreateUser" method.

    Right before processing any request to Membership table (such as select/insert/update/delete), Application is retrieved by applicationName.

    For example, inside the Membership.CreateUser method, QueryHelper.GetApplication is called right before creating a new user.

    Application application = QueryHelper.GetApplication(membershipEntity, 
       applicationName);
    
    // QueryHelper.GetApplication
    internal static Application GetApplication(MembershipEntities ctx, 
       string applicationName)
    {
        ObjectParameter[] objectParameter = new ObjectParameter[1];
        objectParameter[0] = new ObjectParameter("name", 
           applicationName.ToLowerInvariant());
    Application application = ctx.CreateQuery<Application>(
    "select value a FROM Applications as a WHERE ToLower(a.ApplicationName) = @name",
    objectParameter).FirstOrDefault<Application>();
        return application;
    }
    

    If application is null, it is created an application like this -

    internal static Application CreateApplication(MembershipEntities ctx, 
       string appName)
    {
       Application application = new Application();
       application.ApplicationId = Guid.NewGuid();
       application.ApplicationName = appName;
       ctx.Applications.AddObject(application);
       Application application1 = application;
       return application1;
    }
    

    About code is from ASP.NET Universal Providers. Legacy Membership Provider uses Store Procedure, but the logic is almost same.