ist das, wie ich meine Ansprüche bekommen sollte?
AFAIK, können Sie ControllerBase.HttpContext.User
oder ControllerBase.User
nutzen für die System.Security.Claims.ClaimsPrincipal
für den aktuellen Benutzer abgerufen werden. Details können Sie den ähnlichen issue1 und issue2 folgen.
Und meine zweite Frage ist, wie füge ich Ansprüche an diesen Benutzer?
Wie Sie sagten, verwenden Sie ASP.NET Core 2.0 mit Azure AD v2.0. Ich nahm an, wenn UseOpenIdConnectAuthentication
verwenden, können Sie die zusätzlichen Ansprüche unter OnTokenValidated
hinzufügen wie folgt:
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = Configuration["AzureAD:ClientId"],
Authority = string.Format(CultureInfo.InvariantCulture, Configuration["AzureAd:AadInstance"], "common", "/v2.0"),
ResponseType = OpenIdConnectResponseType.IdToken,
PostLogoutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"],
Events = new OpenIdConnectEvents
{
OnRemoteFailure = RemoteFailure,
OnTokenValidated = TokenValidated
},
TokenValidationParameters = new TokenValidationParameters
{
// Instead of using the default validation (validating against
// a single issuer value, as we do in line of business apps),
// we inject our own multitenant validation logic
ValidateIssuer = false,
NameClaimType = "name"
}
});
private Task TokenValidated(TokenValidatedContext context)
{
/* ---------------------
// Replace this with your logic to validate the issuer/tenant
---------------------
// Retriever caller data from the incoming principal
string issuer = context.SecurityToken.Issuer;
string subject = context.SecurityToken.Subject;
string tenantID = context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
// Build a dictionary of approved tenants
IEnumerable<string> approvedTenantIds = new List<string>
{
"<Your tenantID>",
"9188040d-6c67-4c5b-b112-36a304b66dad" // MSA Tenant
};
o
if (!approvedTenantIds.Contains(tenantID))
throw new SecurityTokenValidationException();
--------------------- */
var claimsIdentity=(ClaimsIdentity)context.Ticket.Principal.Identity;
//add your custom claims here
claimsIdentity.AddClaim(new Claim("test", "helloworld!!!"));
return Task.FromResult(0);
}
Dann habe ich den folgenden Code, um die Benutzer Ansprüche abzurufen:
public IActionResult UserInfo()
{
return Json(User.Claims.Select(c=>new {type=c.Type,value=c.Value}).ToList());
}
Test:
Darüber hinaus könnten Sie sich auf diese s beziehen reichlich Integrating Azure AD (v2.0 endpoint) into an ASP.NET Core web app.