0

I am having trouble getting my .NET Core site to redirect to my identity server for authentication when accessing a page. When I run the site locally this works fine. However, when I deploy the site it no longer works.

Here is the code for the application startup (just for the authentication)

public void ConfigureServices(IserviceCollection services) {
  services
    .AddAuthentication(options => {
      options.DefaultScheme => "Cookies";
      options.DefaultChallengeScheme = "oidc";
    })
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options => {
      options.Authority = "IdentityServerUrl";
      options.ClientId = "clientId";
      options.ResponseType = "code";
      options.SaveTokens = true;
      options.Scope.Add("scope");
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
  app.UseAuthentication();
  app.UseAuthorization();

  app.UseEndpoints(endpoints => endpoints.MapRazorPages().RequireAuthorization());
}

And then in our page:

public class IndexModel : PageModel {
  
  public async Task OnGetAsync() {
    var authentication = await this.HttpContext.AuthenticateAsync();
    var accessTokenJwt = authentication.Properties.Items[".Token.access_token"];
  }
}

This is all working correctly when run locally. It is successfully redirecting to the identity server, logging in, returning to the application, and setting the cookies.

However, when this is deployed to a webserver it is not working at all. The await this.HttpContext.AuthenticateAsync(); is called and just immediately returns null. This results in an exception being thrown on the line below.

Any help with this would be much appreciated. Thanks in advance.

Michael Sayers
  • 432
  • 1
  • 3
  • 12
  • how do you deploy? whats the urls? do u use https? – nahidf Oct 14 '20 at 03:35
  • @nahidf We deploy using Microsoft tfs. A DotNetCoreCLI publish to an internal webserver. URLs are not included because of security reasons. But the URL for the identity server does not change between running locally and on the webserver. We do use https. and we have `UseHttpsRedirection` in the application startup. – Michael Sayers Oct 14 '20 at 09:10

0 Answers0