0
嘿,我加入隊伍,我ApplicationUser這樣的:用戶身份的變化自定義字段
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual ICollection<Function> Functions { get; set; }
public virtual ICollection<Participation> Participations { get; set; }
public virtual ICollection<Team> Teams { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Mobile { get; set; }
public DateTime Birthdate { get; set; }
}
在我的控制器我想添加一個新的團隊,爲用戶:
ApplicationUser user = UserManager.GetUserById(dto.PersonId[0]);
if (user != null)
{
if (user.Teams == null)
{
user.Teams = new List<Team>();
}
user.Teams.Add(mannschaft);
mannschaft.UsersId.Add(user?.Id);
uow.SaveChanges();
Team team = uow.RepTeam.Get(t => t.Bezeichnung == dto.Name).FirstOrDefault();
return Request.CreateResponse(HttpStatusCode.Created, team?.Id);
}
所以世界上沒有問題該團隊被添加到IdentityUser,但是當我想打電話給不同的控制器中的所有團隊:
ApplicationUser user = UserManager.GetUserById(userid);
ICollection<DtoTeam> foo = new List<DtoTeam>();
if (user != null)
{
foreach (var teams in user.Teams)
{
foo.Add(uow.RepTeam.GetByKey(teams).ToDto());
}
}
return foo;
它說,有在用戶沒有團隊...
您在混合環境中。有你的身份上下文(其中包含AspNet ...表),還有你的自定義上下文,它應該包含用戶,團隊,功能,參與者等表。身份是關於識別用戶。這與您的自定義上下文無關。你應該嘗試分離這些問題。 –
謝謝你的快速回答。我明白了,但在我的應用程序中,用戶可以在一個團隊中,所以我應該添加一個新的實體人員來保存身份標識和團隊等等?我已經考慮過這個解決方案,但不知道它的好處。 –
是的,用自己的用戶表設置一個獨立的自定義模型。嚴格地說,身份模型只是關於身份。您甚至可以將Identity表存儲在不同的數據庫中,或者您可以使用外部服務來登錄(如IdentityServer)。因此,自定義模型實際上對身份一無所知,並且Identity模型對自定義模型一無所知。這意味着兩者之間沒有關係。除此之外,您可以將登錄用戶鏈接到自定義用戶。您可以通過使用用戶標識添加聲明來完成此操作。 –