[英]Adding Claims in the MVC 5 app with Owin and windows authentication
我正在開發一個由owin實現的身份驗證和表單身份驗證的mvc 5 Web應用程序。
開箱即用,可以很好地處理索賠要求。 現在,我正在嘗試使用Windows身份驗證來登錄系統
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
// app.CreatePerOwinContext(ApplicationDbContext.Create);
// app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//// app.UseGoogleAuthentication(
// clientId: "000-000.apps.googleusercontent.com",
// clientSecret: "00000000000");
}
我已經評論了所有提供者。
我正在嘗試在登錄過程中推送聲明,但是用戶( window.identity.principal
)已通過authenticationmanger.current.user.Isauthenticated
驗證,可以通過authenticationmanger.current.user.Isauthenticated
檢查。
我正在嘗試登錄,但是聲明沒有被推送,但是我可以看到用戶的Claims中存在一個聲明列表,甚至認為沒有觸發sign命令。 就像Windows身份驗證中的owin已經知道誰是當前用戶及其名稱一樣,並且也聲明了所有權。 但是我希望將一些自定義的一次性聲明推送到我無法實現的現有列表中。
現有的所有索賠都是系統索賠類型,這使我懷疑是否可以修改索賠。
如何修改或更新或擴展現有的索賠清單?
我嘗試了權利要求的撤消方法,它可以很好地用於表單身份驗證。
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
// Add more custom claims here if you want.
var claims = new Collection<Claim>
{
new Claim("Surname",user.ApplicantName),
new Claim("ApplicantId",user.ApplicantId),
new Claim("AccessCodeId",user.AccessCodeId),
new Claim ( "Registered", "YES")
};
identity.AddClaims(claims);
var principal = new ClaimsPrincipal(identity);
// turn into Principal
HttpContext.User = principal;
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
我知道這個問題已經很老了,但是我最近在Nancyfx框架上也遇到了同樣的問題。 這也可能對其他人有幫助。
您只需要獲取當前身份並向其中添加新的聲明即可。 請注意,GetRolesForUser只是一個自定義方法。 您也可以添加角色以外的聲明類型。
string[] roles = GetRolesForUser(User.Identity.Name);
var id = ClaimsPrincipal.Current.Identities.First();
foreach (var role in roles)
{
id.Claims.Add(new Claim(ClaimTypes.Role, role));
}
我從BrockAllen找到了這個主意
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.