Authentication Question #367

Closed
opened 2025-08-09 17:16:01 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @NeilIrvingMacc on 10/12/2017

I have an Angular4 app where some of it will be public and will not require login

Other bits will require login

On the WebApi controllers which need someone to be authenticated I have done this:

[Authorize]
[Route("api/[controller]")]
public class xyz: Controller
{
[HttpPost("abc")]
public string abc([FromBody]string s)
{

}
}

If I login by going to /Auth/Login which is an MVC page I can login
and the WebAPI method above works when called from my Angular4 route

If I do not login manually as above the Angular code tries to
call the Api method above gets a HTTP 302 then tries to redirect /Auth/Login

it then gets an error 'Unexpected token <' this is because its loaded
/Auth/Login via the code below, how do I get the Angular to redirect to the login page? I am thinking maybe I have to catch the HTTP 302 in the code below

this.http.post('/api/xyz/abc',
body, options).subscribe(
res => {
this.extractData(res)
});

I need to use MVC for the login as this website will be a mix of MVC and Angular4. The MVC will be all the admin stuff and I do not want to do that in Angular

*Originally created by @NeilIrvingMacc on 10/12/2017* I have an Angular4 app where some of it will be public and will not require login Other bits will require login On the WebApi controllers which need someone to be authenticated I have done this: [Authorize] [Route("api/[controller]")] public class xyz: Controller { [HttpPost("abc")] public string abc([FromBody]string s) { } } If I login by going to /Auth/Login which is an MVC page I can login and the WebAPI method above works when called from my Angular4 route If I do not login manually as above the Angular code tries to call the Api method above gets a HTTP 302 then tries to redirect /Auth/Login it then gets an error 'Unexpected token <' this is because its loaded /Auth/Login via the code below, how do I get the Angular to redirect to the login page? I am thinking maybe I have to catch the HTTP 302 in the code below this.http.post('/api/xyz/abc', body, options).subscribe( res => { this.extractData(res) }); I need to use MVC for the login as this website will be a mix of MVC and Angular4. The MVC will be all the admin stuff and I do not want to do that in Angular
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#367
No description provided.