我真的不明白如何使用这个身份的东西,下面我尽我所能,我没有错误,但它什么都不做,我想知道为什么。 我也检查了这个:
Adding Role dynamically in new VS 2013 Identity UserManager
Add User to Role ASP.NET Identity
数据库中的表被修改我有:Id(自动增量,主键),RoleId,UserId - 默认情况下都没有主键。
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var roleStore = new RoleStore<IdentityRole>();
var manager = new UserManager<IdentityUser>(userStore);
var RoleManager = new RoleManager<IdentityRole>(roleStore);
var user = new IdentityUser { UserName = CreateUserWizard1.UserName };
var role = new IdentityRole { Id = RadioButtonList1.SelectedValue};
//Create new user and try to store in DB.
if (CreateUserWizard1.Password.ToString() == CreateUserWizard1.ConfirmPassword.ToString())
{
try
{
IdentityResult result = manager.Create(user, CreateUserWizard1.Password.ToString());
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
var selectedRole = RadioButtonList1.SelectedValue;
var currentUser = manager.FindByName(user.UserName);
var roleResult = manager.AddToRole(currentUser.Id, selectedRole);
authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);
.
.
我不是在寻找使用visual studio制作的经典解决方案。我有SQL的用户配置文件,并且工作正常,但我的角色现在搞砸了。如果我手动放入SQL表RoleId和UserId,那么它们工作正常。
答案 0 :(得分:-1)