0
我們有一個使用adfs(ws-federation)身份驗證設置的mvc 5項目。現在,如果數據庫中的標誌爲真,我們想向用戶添加一個自定義聲明。哪裏可以做到這一點?Asp.net mvc 5;在哪裏添加自定義聲明? (adfs登錄)
我們有一個使用adfs(ws-federation)身份驗證設置的mvc 5項目。現在,如果數據庫中的標誌爲真,我們想向用戶添加一個自定義聲明。哪裏可以做到這一點?Asp.net mvc 5;在哪裏添加自定義聲明? (adfs登錄)
This博客條目讓我們在正確的方向。索賠需要在用戶驗證後的最後一分鐘注入。
Startup.Auth.cs應該是這個樣子:
app.UseActiveDirectoryFederationServicesBearerAuthentication(
new ActiveDirectoryFederationServicesBearerAuthenticationOptions
{
MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
TokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
//NameClaimType = "User-Principal-Name",
//SaveSigninToken = true
},
//Inject custom claims from Database
Provider = new OAuthBearerAuthenticationProvider()
{
OnValidateIdentity = async context =>
{
string UPN = context.Ticket.Identity.FindFirst(ClaimTypes.Upn).Value;
UPN = UPN.Remove(UPN.Length - 12);
User user = new User();
//user = GetUserData("[email protected]");
user = GetUserData(UPN); //Get user data from your DB
context.Ticket.Identity.AddClaim(
new Claim("UserName", user.UserName.ToString(), ClaimValueTypes.String, "LOCAL AUTHORITY"));
}
}
});