當用戶登錄到系統時,我正在呼叫用戶訪問。在我的用戶訪問中包括UserName,UserRole,UserId,ReqAccess,ViewAccess等
。在我的索賠中,我可以添加User Id和User Names,但我想在單獨的索賠中添加其他訪問權限,但當我輸入claims.Add(new Claim(ClaimTypes.時,串列只顯示列出的專案。我如何添加我自己的索賠,并添加它們?
public void SignInUser(string username, string userRole, string userid, bool isPersistent)
{
//初始化。
var claims = new List<Claim> ();
try
{
// Setting
claims.Add(new Claim(ClaimTypes.Name, username))。
claims.Add(new Claim(ClaimTypes.Role, userRole))。
claims.Add(new Claim("UserId"/span>, userid))。
var claimIdenties = new ClaimsIdentity(claim, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
//Sign In.
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, claimIdenties) 。
var identity = new ClaimsIdentity(claim, DefaultAuthenticationTypes.ApplicationCookie)。
var claimsPrincipal = new ClaimsPrincipal(identity)。
Thread.CurrentPrincipal = claimsPrincipal。
}
catch (Exception ex)
{
// Info
throw ex;
}
uj5u.com熱心網友回復:
我這樣做了,而且作業得很好:
var user = await UserManager.FindAsync(model.Email, model.Password)。
if (user != null)
{
var identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie) 。
identity.AddClaims(new[] 。
{
new Claim("FullName", user.FullName) 。
new Claim("roleId", user.Roles.FirstOrDefault() .RoleId)。
});
AuthenticationManager.SignIn( new AuthenticationProperties() { IsPersistent = true }, identity);
return View()。
}
獲取用戶詳情并保存在索賠中。
uj5u.com熱心網友回復:
你也許可以試試這樣的方法 claims.Add(new Claim("customClaim", "claimValue"));
因此,Claim類應該有一個多載的建構式,它接收一個字串值作為索賠型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/319229.html
標籤:
