Skip to content

Blazor Server Authentication not working after publishing to Azure (linux) #25430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
drma-tech opened this issue Aug 30, 2020 · 23 comments
Closed
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly
Milestone

Comments

@drma-tech
Copy link

https://site.azurewebsites.net/
signin-google
?state=xxx
&code=yyy
&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
&authuser=0
&prompt=none

Sorry, there is nothing at this address.

This error occurs after trying to log in to google in a blazor app after publishing in azure linux (the same did not happen in the windows environment).

logging in normally (user + password) works, but cannot communicate with the API.

@blowdart blowdart added the area-blazor Includes: Blazor, Razor Components label Aug 31, 2020
@javiercn javiercn added area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer and removed area-blazor Includes: Blazor, Razor Components labels Aug 31, 2020
@javiercn
Copy link
Member

@drma-dev thanks for contacting us.

@blowdart this is not Blazor server specific since auth happens before the Blazor application starts (this is server-side Blazor). From what I can tell, the callback is not hitting the auth endpoint and going through the catch-all route that SSB defines instead. Note that it if goes there is because no other endpoint/handler was available.

@drma-tech
Copy link
Author

drma-tech commented Aug 31, 2020

It started after a certain time. I'm not sure if it was from a certain version of blazor or something that happened on azure.

another mistake is that if you log in with email and password (without google), it cannot call the API, because it cannot recognize the user's token or authentication.

@blowdart
Copy link
Contributor

blowdart commented Sep 3, 2020

This doesn't sound generic to me @javiercn - perhaps something with the identity server integration give the inability to call the api.

regardless, @drma-dev what's in the server logs? Can you up the verbosity and try?

@blowdart blowdart added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Sep 3, 2020
@javiercn
Copy link
Member

javiercn commented Sep 3, 2020

@blowdart Blazor server doesn't use Identity Server (at least our templates). We only use it on Blazor WebAssembly.

Blazor server apps use whatever auth mechanism you want to use for a regular Web Application, all the auth happens before the Blazor Server app starts.

What is likely happening there is what I mentioned above, the auth callback endpoint is somehow not correctly configured, and it's going through the fallback route that server-side Blazor defines, but the issue is that the auth handler is not correctly configured in some way.

@blowdart
Copy link
Contributor

blowdart commented Sep 3, 2020

OK in that case, startup.cs pleas @drma-dev

@drma-tech
Copy link
Author

Even using the template (with authentication option) without changing anything in the code (forgetting this google issue), it will give an error when accessing the api with [Authorize].

And it only happens in azure (linux environment), if I publish in azure (windows environment) it works normally.

I believe that both errors (API and google) are related to the same thing.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Sep 3, 2020
@blowdart
Copy link
Contributor

blowdart commented Sep 4, 2020

OK what template, what error? What's in the server logs?

@blowdart blowdart added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Sep 4, 2020
@drma-tech
Copy link
Author

drma-tech commented Sep 4, 2020

https://github.com/drma-dev/BlazorApp1

https://app-verusdate-beta.azurewebsites.net/ (fetch data page)

I don't know how to catch errors (verbosity) in azure

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Sep 4, 2020
@javiercn
Copy link
Member

javiercn commented Sep 4, 2020

@drma-dev thanks for the repro.

This turned out to be Blazor webassembly, not server-side Blazor. Moving to the blazor area

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly and removed area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer labels Sep 4, 2020
@javiercn
Copy link
Member

javiercn commented Sep 4, 2020

@drma-dev just co confirm, your scenario is that you have a hosted Blazor WebAssembly application that uses individual auth (Identity + Identity Server) and you are trying to offer the option to users to authenticate using their google credentials as part of logging in with Google, and it's failing after you click login with google on the UI and tries to come back to the application?

@drma-tech
Copy link
Author

drma-tech commented Sep 4, 2020

Yes, but this error only occurs when it is published in azure (web app - linux environment).

And if you log in using the traditional method (email + password) the login works, but the APIs stop responding due to authentication failure.

@javiercn
Copy link
Member

javiercn commented Sep 4, 2020

That seems like two separate issues to me:

  • The authority/issuer are out of sync, try setting up the authority and the issuer explicitly in the IdentityServer options and JwtBearer options respectively,

  • Your google callback-endpoint is not correctly configured and is not being picked up. That doesn't have anything to do with Blazor.

@drma-tech
Copy link
Author

No. I believe it is a limitation of the linux web app

@javiercn
Copy link
Member

javiercn commented Sep 4, 2020

No. I believe it is a limitation of the linux web app

Not sure what you mean by this

@drma-tech
Copy link
Author

localhost does not have these errors.
azure web app (windows environment) does not have these errors.
azure web app (linux environment) has these errors.

@drma-tech
Copy link
Author

@javiercn is it clear now?

I haven't found any place that can report azure bugs, so I'm here

@javiercn javiercn self-assigned this Sep 7, 2020
@javiercn javiercn added the Docs This issue tracks updating documentation label Sep 7, 2020
@javiercn javiercn added this to the 5.0.0 milestone Sep 7, 2020
@carlingkirk
Copy link

I was experiencing this issue - the "/signin-google" request was being served from index.html. The fix is to add the endpoint into the list of exclusions in onFetch in service-worker.published.js

In Client\wwwroot\service-worker.published.js:

async function onFetch(event) {
    let cachedResponse = null;
    if (event.request.method === 'GET') {
        // For all navigation requests, try to serve index.html from cache
        // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
        const shouldServeIndexHtml = event.request.mode === 'navigate'
          && !event.request.url.includes('/connect/')
          && !event.request.url.includes('/signin-google')
          && !event.request.url.includes('/Identity/');

        const request = shouldServeIndexHtml ? 'index.html' : event.request;
        const cache = await caches.open(cacheName);
        cachedResponse = await cache.match(request);
    }

    return cachedResponse || fetch(event.request);
}

@drma-tech
Copy link
Author

thanks. you save my life.

@drma-tech
Copy link
Author

drma-tech commented Oct 18, 2020

Although I am able to login, there is still an error when accessing a protected api. did you go through this @carlingkirk?

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: net_http_message_not_success_statuscode, 401, Unauthorized
System.Net.Http.HttpRequestException: net_http_message_not_success_statuscode, 401, Unauthorized
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__9`1[[VerusDate.Shared.WeatherForecast[], VerusDate.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at VerusDate.Client.Pages.FetchData.OnInitializedAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

www-authenticate: Bearer error="invalid_token", error_description="The issuer 'https://....azurewebsites.net' is invalid"

@carlingkirk
Copy link

@drma-dev Yes - unfortunately I had to set it explicitly in AddIdentityServer - see

services.AddIdentityServer(opt =>
{
    opt.IssuerUri = "https://....azurewebsites.net";
})

@drma-tech
Copy link
Author

thanks

@mcguireuk
Copy link

I was experiencing this issue - the "/signin-google" request was being served from index.html. The fix is to add the endpoint into the list of exclusions in onFetch in service-worker.published.js

In Client\wwwroot\service-worker.published.js:

async function onFetch(event) {
    let cachedResponse = null;
    if (event.request.method === 'GET') {
        // For all navigation requests, try to serve index.html from cache
        // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
        const shouldServeIndexHtml = event.request.mode === 'navigate'
          && !event.request.url.includes('/connect/')
          && !event.request.url.includes('/signin-google')
          && !event.request.url.includes('/Identity/');

        const request = shouldServeIndexHtml ? 'index.html' : event.request;
        const cache = await caches.open(cacheName);
        cachedResponse = await cache.match(request);
    }

    return cachedResponse || fetch(event.request);
}

Yes, worked a treat. but I'd suggest to make the string '/signin-' this way if you add multi providers which usually default to signin-google, signin-microsoft etc these will all work.

@pranavkm pranavkm modified the milestones: 5.0.0, 6.0-preview1 Nov 11, 2020
@javiercn
Copy link
Member

You need to specify the issuer explicitly when deploying to App Service on LInux.

            services.AddAuthentication()
                .AddIdentityServerJwt();

            services.Configure<JwtBearerOptions>(IdentityServerJwtConstants.IdentityServerJwtBearerScheme, options =>
            {
                options.Authority = "https://my-service.azurewebsites.net";
            });

@guardrex can we add a note to the docs?

@javiercn javiercn removed the Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. label Jan 15, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly
Projects
None yet
Development

No branches or pull requests

6 participants