Base solution for your next web application
Open Closed

Getting 401 (Unauthorized) #5147


User avatar
0
thor created

Morning,

I have a bit of an issue which I cannot seem to resolve by myself. I am using the Core+Angular template.

Firstly, I have created a number of AppServices in a separate module (i.e. not in the standard MyProject*.Application project). These are all working fine when using the Swagger UI and Postman.

Then I ran nswag/refresh.bat and referred to the generated ProxyServices as providers in service-proxymodule.ts

I have then created a couple of Components.

  1. They are declared in main.module.ts
  2. The ProxyServices are included as providers in main.module.ts
  3. Added a path for my components in main-routing.module.ts
  4. Included a link to my page in navigation

When I open the page in a browser I get 401 (Unauthorized). This is because the server is complaining the user hasn't been logged in.

Using Fiddler I notice that my component does not include any auth header tokens.

Below is some sample code from my component:

import { Component, Injector, ViewEncapsulation, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PatientsServiceProxy, CreateOrEditPatientDto, GetPatientForEditOutput} from '@shared/service-proxies/service-proxies';
declare let d3, Datamap: any;

@Component({
    templateUrl: './patient-details.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class PatientDetailsComponent extends AppComponentBase implements OnInit {

    ngOnInit(): void {

    }

    constructor(
        injector: Injector,
        private _patientsServiceProxy: PatientsServiceProxy

    ) {
        super(injector);
    }

    active: boolean = false;
    saving: boolean = false;

    patientId: string;

    patient: CreateOrEditPatientDto = new CreateOrEditPatientDto();

    savePatient(): void {
        this.saving = true;
        this._patientsServiceProxy.createOrEdit(this.patient)
            .finally(() => { this.saving = false; })
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
            });
    }
}

How do I get my component to include the auth? I thought that proxy services would solve that...


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @thor,

    Yes, auth token must be added to header automatically.

    1. Have you upgdated your solution recently (Maybe the version of Angular) ?
    2. Are the initial services (pages) working for you ? User list page for example ?

    If initial pages are working but not yours, could you send your project via email to <a href="mailto:[email protected]">[email protected]</a> ?

  • User Avatar
    0
    thor created

    Yes, I have updated Angular recently. And yes the initial/standard pages are all working.

    I will send through the specific feature branch I have been playing around with to the email address below. Do you want all of it?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We have found the problem. You need to remove the providers array of main.module.ts which is not necessary. Because of this, your services are not intercepted and auth token not added to request headers.

  • User Avatar
    0
    thor created

    Thank you very much!

  • User Avatar
    0
    alper created
    Support Team

    thanks for the feedback ;)