Getting 401 when clicking Print -> PDF

Stimulsoft Reports.ANGULAR discussion
Ali.A
Posts: 12
Joined: Thu Mar 10, 2022 6:34 pm

Getting 401 when clicking Print -> PDF

Post by Ali.A »

Hello,
I have a angular viewer in Angular APP(v11), talking to DotNet core API 6. As per our requirements we authorise all transactions to backend using access token.
At initial load both initViewer and ViewerEvent gets called along with token and load the report okay.
Once loaded if I to try clicking Print -> PDF, it makes a call to ViewerEvent and fails with 401, as this request doesn't has access token to it.
We use HTTP interceptor to add token to all the calls made from the app.
Please help!
Ali
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Getting 401 when clicking Print -> PDF

Post by Lech Kulikowski »

Hello,

Please send us a sample project that reproduces the issue for analysis.

Thank you.
Ali.A
Posts: 12
Joined: Thu Mar 10, 2022 6:34 pm

Re: Getting 401 when clicking Print -> PDF

Post by Ali.A »

Please see attached sample, I have added a key authorization when make call to "ViewerEvent" endpoint.
The angular app has http interceptor code which add the key to each call being made to the server.
At initial load it works and key gets passed around.

But as soon as you click Pint -> PDF, it some how bypass the interceptor and I get 401.

Is there an event like 'onBeginTransaction' which I can hook in.

Thanks
Attachments
Samples-Angular-master.zip
(28.4 MiB) Downloaded 180 times
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Getting 401 when clicking Print -> PDF

Post by Lech Kulikowski »

Hello,

We need some additional time to investigate the issue, we will let you know about the result.

Thank you.
Vadim
Posts: 362
Joined: Tue Apr 23, 2013 11:23 am

Re: Getting 401 when clicking Print -> PDF

Post by Vadim »

Hello.

If PDF preview we use Form post functional, so using HTTP headers is not available here.
Please use next solution:
In

Code: Select all

<stimulsoft-viewer-angular
add

Code: Select all

[postParametersFunction]="getPostParameters"
In AppComponent add function:

Code: Select all

public getPostParameters(data): any {
        return { 'ApiKey': `2ef25513-c765-4e62-8512-15e50d533db8` };
    }
In AuthorisationKeyHandler use Form parameters:

Code: Select all

private void SucceedRequirementIfAuthKeyPresentAndValid(AuthorizationHandlerContext context, AuthorisationKeyRequirement requirement)
        {
            var authKey = this.httpContextAccessor.HttpContext.Request.Form["ApiKey"].FirstOrDefault();
            if (authKey != null && requirement.AuthorisationKeys.Any(requiredAuthKey => authKey == requiredAuthKey))
            {
                context.Succeed(requirement);
            }
        }
Ali.A
Posts: 12
Joined: Thu Mar 10, 2022 6:34 pm

Re: Getting 401 when clicking Print -> PDF

Post by Ali.A »

That seems to be okay with above approach. I have another set of APIs which works with JWT Authorisation. Considering above I added following:

Code: Select all

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
    options.Events = new JwtBearerEvents
    {
        OnMessageReceived = context =>
        {
            if (context.Request.Path.StartsWithSegments("/reportviewer", StringComparison.OrdinalIgnoreCase))
            {
                if (context.Request.Form.TryGetValue("access_token", out var accessToken))
                {
                    context.Token = accessToken;
                }
            }
            return Task.CompletedTask;
        },
    };
});
With above I mange to fix the 401, but now I am getting this at clientside. see attached image
Attachments
error.png
error.png (63.94 KiB) Viewed 6896 times
Vadim
Posts: 362
Joined: Tue Apr 23, 2013 11:23 am

Re: Getting 401 when clicking Print -> PDF

Post by Vadim »

Hello.

You need to enable CORS, for example like:
1.png
1.png (116.31 KiB) Viewed 6889 times
Ali.A
Posts: 12
Joined: Thu Mar 10, 2022 6:34 pm

Re: Getting 401 when clicking Print -> PDF

Post by Ali.A »

Thanks, I already have CORS settings enabled as per above and I am still getting the same error....

ERROR DOMException: Blocked a frame with origin "http://localhost:53422" from accessing a cross-origin frame.
Vadim
Posts: 362
Joined: Tue Apr 23, 2013 11:23 am

Re: Getting 401 when clicking Print -> PDF

Post by Vadim »

Hello.

We don't exactly know your configuration, so we haven't common solution for it.
What you need to do is to enable CORS.
Ali.A
Posts: 12
Joined: Thu Mar 10, 2022 6:34 pm

Re: Getting 401 when clicking Print -> PDF

Post by Ali.A »

I definitely have exact same CORS policy enabled as above. Could this is be an issue if you're debugging it locally.
I am going to host the site to see if it fixes the issue.
Post Reply