So I have an Angular and C# Web API 2 app that uses Microsoft Authentication (msal.js) for the login. Now I'm trying to save data and I need the details of the current logged user. Is there a way to get the logged user from the C# Controller?
I can do this in Angular, but I think it's not secured to do it from the client side so I was thinking if maybe there was a way that the backend knows who's the logged user.
Thanks in advance!
EDIT
Startup.Auth.cs
var tvps = new TokenValidationParameters
{
ValidAudience = "the id given from Microsoft Graph Registration", //xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
ValidateIssuer = false,
};
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new Microsoft.Owin.Security.Jwt.JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"))
});
frontend-login.ts
let userAgentApp = new UserAgentApplication(clientId, null,
(errorDes: any, token: any, error: any, tokenType: any) => {
userAgentApp.acquireTokenSilent(["user.read"]).then((token: string) => {});
}, { redirectUri: 'http://localhost:port/signin-microsoft' });
userAgentApp.loginPopup(["user.read"]).then((token: string) => {
//store the token and redirect to home page
});
EDIT
I am using it when accessing the API like this:
this.headers.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'));
this.http.get(`${this.url}`, { headers: this.headers })
.map((response: Response) => { return response.json() })
FINAL EDIT
I posted another question regarding this and this problem was answered there. I am posting the link below in case someone needs it in the future:
var user = User.Identity//store the token and redirect to home pagesuggests that you are storing the token somewhere for later use. Can you post some code showing how you are using the stored token when contacting your web api later on?