OWIN Login slow performance >30 Seconds

2.4k views Asked by At

I have a big performance issue when i try to login with my application. The application is a Web api app hosted in Azure. I use react js for my front end.

And in my SimpleAuthorizationprovider i am fetching the user and adding claims to the ClaimsIdentity property. If i debug it locally i only have slow performance issue the first time i login but the next time i login the login is instant access. When i upload it to my test environment in Azure the problem is there every request.

I have switched on the "Always on" property in Azure for the web app. I have timed the request and the thing that takes a lot of time is the this piece of code in SimpleAuthorizationprovider

  context.Validated(ticket);

It takes in my test environment 15 sek to just authorize the user. There is maybe some delays in my front end as well. IN my front end we are using superagent to make requests to the web api.

Here is my configuration in my startup class:

public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);

        ConfigureOAuth(app);
        var physicalFileSystem = new PhysicalFileSystem(@"./Html");
         var options = new FileServerOptions
        {
            EnableDefaultFiles = true,
            FileSystem = physicalFileSystem,
            StaticFileOptions =
            {
                FileSystem = physicalFileSystem,
                ServeUnknownFileTypes = true
            },
            DefaultFilesOptions = {DefaultFileNames = new[] {"Startup.html"}}
        };
        app.UseFileServer(options);

        // Get your HttpConfiguration.
        var config = GetInjectionConfiguration(); 
        config.MessageHandlers.Add(new QueryStringBearerToken());

        WebApiConfig.Register(config);

        // Setup WebApi
        app.UseWebApi(config);
        SetupWebApi(config);

        Application_Start(config);
        config.EnsureInitialized();
    }

  public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/api/token"),
            AuthorizeEndpointPath = new PathString("/Login"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(120),
            Provider = new SimpleAuthorizationServerProvider(),
            RefreshTokenProvider = new ApplicationRefreshTokenProvider(),
            AllowInsecureHttp = true,

        };

        var oAuthBearerOptions = new OAuthBearerAuthenticationOptions
        {
            Provider = new QueryStringOAuthBearerProvider(),
            AccessTokenProvider = new AuthenticationTokenProvider(),
        };

        app.UseOAuthBearerAuthentication(oAuthBearerOptions);
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
    }

EDIT:

I can now say that the context.validated(ticket) is not the problem. I have connected application insights in Azure to see if i can get any more useful information from there. My guess is still that the api process is not "warmed" up before the request hits and that results in that the process needs to boost everytime. When i debug it locally it takes about 10 sek for the first request to hit my token endpoint. After that request i sent another one and everything works as expected. Some more digging needs to be done here ;)

3

There are 3 answers

1
Andreas Jangefalk On BEST ANSWER

The problem was that the base class AuthContext had given the connectionstring the name AuthContext but in my webconfig my connectionstring was given the name Default so when the application started the first 5 or 6 request were just bouncing of the because there was no valid connection to the databse.

So here is my solution

public AuthContext()
            : base("Default")
        {

        }
1
Sven On

Did you try to remote debug? You must enable this on the Azure Web App in the Azure Portal and then from Visual Studio with the Cloud Explorer find your Web App and choose for Remote Debugging. Make sure you published your website in DEBUG mode.

Perhaps this can help you pinpoint the problem.

1
Rajat On

I don't see why you need to

Application_Start(config); config.EnsureInitialized();

inside your configuration. Please remove this.