Reputation: 15237
I have a .NET Core API that has an Artist.cs
entity. This application has no Authentication or Authorization logic. All that is delegated to IdentityServer4 coupled with ASP.NET Identity.
public class Artist
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserId { get; set; }
public DateTime CreatedAt { get; set; }
}
I wish to have an endpoint on my API that shows the most recent submissions /artists/latest
with a response like:
[
{
"artist": "Ray Charles",
"username": "John Doe"
},
{
"artist": "BB King",
"username": "Mary"
}
]
The problem is, my API database does not have the username! It only stores the user Id. How do I get the username from the UserId?
Upvotes: 0
Views: 364
Reputation: 818
Why that application doesn't have Authorization?
When you generate the token on IdentityServer, the client must ask for "profile" scope, and IdentityServer must allow it.
Then you will have access to the following profile claims:
Just make sure when you are creating users you are creating the "name" claim. When your API receives the access token, you can access the "name" claim.
Upvotes: 0
Reputation: 2631
You need to access Username (Data stored in another database) from a different API.
Let's get some things straight. There are three different apps involved in this scenario.
Here, the Identity Server and Resource Owner can be the same app in some cases.
There are two ways you can achieve it.
API way
users\{userId}
on the Resource Owner API./artists/latest
endpoint. It needs access to the information of the user x
xyz
) from the response somewhere so it can reuse it in the current request.www.my-api.com/api/users/x
with the token as Authorizatoin: Bearer xyz
. And from the response, it retrieves the required info. You can also make an endpoint where you can send a list of user id and it returns list of user info.
Database way
The other way (Simpler way IMO)
If you have data access layer in place then you can just add it as a dependency of the Client API and use it directly.
If you don't have a DAL then you can just create a library and paste the DBContext in it then reuse it in any project you want.
Upvotes: 1