ためでしたか:
userManager.AddToRole(user.Id, "NameOfRole");
以下のような登録アクションを作成できます。 eredのユーザーは1つの役割から来ます。 Manager
の他の役割を作成し、後でたとえば特定のユーザーを更新することができます。
using (var context = new ApplicationDbContext())
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
//adding a role after register
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, "SimpleUser");
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
}
次に、承認属性でRoles
を使用して承認することができます。
[Authorize(Roles="Manager, SimpleUser, so on..")]
問題はSignInManagerが拡張カスタムユーザーではなくApplicationUserのみを受け入れることです。私はCustomUserを作成して正しい役割に追加することができましたが、今は "彼にサインイン"することができなければなりません。それでSignInManagerのインスタンスを取得して彼にサインインすることは可能ですか? –
joks
@joksはあなたのCustomUserが 'IdentityUser'によって実装されていますか? – Valkyrie
@joksこの記事を見ると、カスタムユーザー用のモデルを作成する必要はありません.AppleUserを使用することができます。この記事のようにクラスに追加できるプロパティを追加したい場合は、http:// stackoverflow .com/questions/28335353/how-to-extend-available-of-user-identity-ID – Valkyrie