Page 1 of 2

Getting 401 when clicking Print -> PDF

Posted: Thu Mar 10, 2022 6:43 pm
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

Re: Getting 401 when clicking Print -> PDF

Posted: Fri Mar 11, 2022 12:32 pm
by Lech Kulikowski
Hello,

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

Thank you.

Re: Getting 401 when clicking Print -> PDF

Posted: Mon Mar 28, 2022 8:52 am
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

Re: Getting 401 when clicking Print -> PDF

Posted: Tue Mar 29, 2022 8:09 pm
by Lech Kulikowski
Hello,

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

Thank you.

Re: Getting 401 when clicking Print -> PDF

Posted: Wed Mar 30, 2022 7:32 am
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);
            }
        }

Re: Getting 401 when clicking Print -> PDF

Posted: Thu Mar 31, 2022 8:55 am
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

Re: Getting 401 when clicking Print -> PDF

Posted: Thu Mar 31, 2022 1:34 pm
by Vadim
Hello.

You need to enable CORS, for example like:
1.png
1.png (116.31 KiB) Viewed 7220 times

Re: Getting 401 when clicking Print -> PDF

Posted: Fri Apr 01, 2022 5:39 am
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.

Re: Getting 401 when clicking Print -> PDF

Posted: Wed Apr 06, 2022 6:47 am
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.

Re: Getting 401 when clicking Print -> PDF

Posted: Thu Apr 07, 2022 4:55 am
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.