ログインするためのサンプルコードは次のとおりです。
if (WebSecurity.Login(playerModel.WorkerID, playerModel.Password))
{
if (Roles.IsUserInRole(User.Identity.Name, "Administrator"))
{
// display the admin panel
ViewBag.Message = "Admin Panel";
return View("Index");
}
else
{
ViewBag.ButtonText = "Log In";
ViewBag.Message = "You are not an Admin user";
return View();
}
}
なんらかの理由で、初めてログインしたときに、ユーザーがテーブル"Administrator"
に正しく表示されているにもかかわらず、ユーザーがログインしていません。Role
私もこれを使ってみました:
Roles.GetRolesForUser();
それは空の配列を返します。ユーザーのログインに制限があり、ポストバックが発生してロールを確認できると想定しています。しかし、回避策があることを願っています(または何か問題があります)。
ポストバック後、メソッドRoles.IsUserInRole
とRoles.GetRolesForUSer
メソッドの両方が正しく機能します。