Closed altair256 closed 7 years ago
Unfortunately, I don't have much time to investigate why this is not working for you. I'd recommend giving the password flow sample a try, as it comes with a resource controller demo that does the same thing: https://github.com/openiddict/openiddict-samples/tree/master/samples/PasswordFlow
Thank you for the answer! I already have a userinfo controller. (As a resource controller) UserInfoController.cs
[Route("api")]
public class UserinfoController : Controller
{
private readonly UserManager<Person> _userManager;
public UserinfoController(UserManager<Person> userManager)
{
_userManager = userManager;
}
//
// GET: /api/userinfo
[Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)]
[HttpGet("userinfo"), Produces("application/json")]
public async Task<IActionResult> Userinfo()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return BadRequest(new OpenIdConnectResponse
{
Error = OpenIdConnectConstants.Errors.InvalidGrant,
ErrorDescription = "The user profile is no longer available."
});
}
var claims = new JObject();
// Note: the "sub" claim is a mandatory claim and must be included in the JSON response.
claims[OpenIdConnectConstants.Claims.Subject] = await _userManager.GetUserIdAsync(user);
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIdConnectConstants.Scopes.Email))
{
claims[OpenIdConnectConstants.Claims.Email] = await _userManager.GetEmailAsync(user);
claims[OpenIdConnectConstants.Claims.EmailVerified] = await _userManager.IsEmailConfirmedAsync(user);
}
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIdConnectConstants.Scopes.Phone))
{
claims[OpenIdConnectConstants.Claims.PhoneNumber] = await _userManager.GetPhoneNumberAsync(user);
claims[OpenIdConnectConstants.Claims.PhoneNumberVerified] = await _userManager.IsPhoneNumberConfirmedAsync(user);
}
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIddictConstants.Scopes.Roles))
{
claims[OpenIddictConstants.Claims.Roles] = JArray.FromObject(await _userManager.GetRolesAsync(user));
}
// Note: the complete list of standard claims supported by the OpenID Connect specification
// can be found here: http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
return Json(claims);
}
When i try get resource .../api/userinfo. I have error 401 Unauthorized, But authorize before successful and I get a token.
I solve the problem. I did not pass the access_token in heades (angular 2) when I try to get resource.
I change auth.service.ts
...
get() {
return this.http.get("api/Accounts")
.map(response => response.json());
}
...
to
...
get() {
return this.http.get("api/Accounts", {
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.accessToken
})
})
.map(response => response.json());
...
Now this work. Thank you for help!
Hello! I use openiddict (1.0.0-beta2-0552). Authentication is successful. but thean i try get userInfo. application answer to me 'user is null'.
BaseController.cs
Startup.cs
AuthorizeController.cs
Person.cs
auth.service.ts (Application is user angular2. May be there be anything wrong?)
I would be very grateful for the help. Thank you.