How to handle ASP.NET Core Identity for external login in microservice? #66678
-
|
I have two projects:
MVC project can use external login and need to add external login user into user web api. The demo code uses How to handle this scenario? I must create endpoint in user web api? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Yes, in your scenario you should handle external login linking inside the User Web API, not in the MVC project. Why?Methods like: _signInManager.GetExternalLoginInfoAsync()
_userManager.AddLoginAsync(user, info)are part of ASP.NET Core Identity and require access to:
Since your MVC project cannot directly access the Identity layer or database, it should NOT perform these operations. Correct Architecture1. MVC Project (Frontend)
2. User Web API (Identity Layer)
Recommended FlowStep 1: External Login (MVC)User authenticates via external provider. Step 2: MVC receives provider dataMVC extracts:
Step 3: MVC calls User APIPOST /api/auth/external-loginPayload example: {
"provider": "Google",
"providerKey": "123456789",
"email": "user@example.com",
"name": "User Name"
}Step 4: User API handles Identity logicInside the User Web API: var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
user = new ApplicationUser
{
UserName = email,
Email = email
};
await _userManager.CreateAsync(user);
}
var loginInfo = new UserLoginInfo(provider, providerKey, provider);
var result = await _userManager.AddLoginAsync(user, loginInfo);Then generate JWT token and return it to MVC. ConclusionYes - you MUST create an endpoint in your User Web API for external authentication. The MVC project should only:
All ASP.NET Core Identity operations like |
Beta Was this translation helpful? Give feedback.
Yes, in your scenario you should handle external login linking inside the User Web API, not in the MVC project.
Why?
Methods like:
are part of ASP.NET Core Identity and require access to:
Since your MVC project cannot directly access the Identity layer or database, it should NOT perform these operations.
Correct Architecture
1. MVC Project (Frontend)
2. User Web API (Ide…