私はアプリケーションでフォーム認証を実装しようとしています。私はいろいろな例を見て、このフォーラムとASP.net MVCで提供されているサンプルと質問を見ましたが、うまく動作しません。フォーム認証:ロール(MVC 4)C#
は、私は私のユーザーを認証するために管理するが、役割は、私は以下のように私のWeb.Config設定している
:-(動作するようには思えません。私のコントローラで
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> </authentication>
は私が設定しましたその後、インデックスのAllowAnonymousにページと、ユーザーが認証された場合は、ログインページにリダイレクトされない場合があり、チェックイン...
[AllowAnonymous]
public ActionResult Index(string sortOrder, string searchString,string currentFilter, int? page)
{
if (!Request.IsAuthenticated)
{
return RedirectToAction("Login", "Account");
}
//Find all the employees
var employees = from s in db.Employees
select s;
//Pass employees to the view (All works fine)
return View(employees.ToPagedList(pageNumber, pageSize));
}
このすべてが動作している100%
私のログインコードは次のようになります。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(User user, string returnUrl)
{
var myUser = db.Users.Where(b => b.UserName == user.UserName).FirstOrDefault();
if(myUser != null)
{
if(myUser.Password==user.Password)
{
//These session values are just for demo purpose to show the user details on master page
//Session["User"] = user;
ICollection<UserAccessLevel> levels = db.UserAccessLevels.Where(b => b.UserId == myUser.UserId).ToList();
//Session["levels"] = levels;
//Let us now set the authentication cookie so that we can use that later.
FormsAuthentication.SetAuthCookie(user.UserName, false);
return RedirectToAction("Index","Employee");
}
}
ViewBag.Message = "Invalid User name or Password.";
return View(user);
}
私はまた、Global.asaxファイルに次のコードを持っている:
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
//let us take out the username now
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
string roles = string.Empty;
using (TrainingContext entities = new TrainingContext())
{
User user = entities.Users.SingleOrDefault(u => u.UserName == username);
roles = "admin";//user.Roles;
}
//Let us set the Pricipal with our user specific details
e.User = new System.Security.Principal.GenericPrincipal(
new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}
catch (Exception)
{
//somehting went wrong
}
}
}
}
私は私のFormsAuthentication_OnAuthenticate実行にログインするとすべてがよさそうだ。私のユーザが設定され、セッションの私の役割もそこにあります...
私は従業員/インデックス画面の詳細をクリックすると、ログイン画面に戻ります。私がログインしているためにクリックした従業員の詳細と管理者の役割としてセットアップされています)
私は問題を解決するために手伝ってください。私はすでに18時間以上それを把握しようとしていた。
あなたは私のコードのほとんどはそこから来て見ることができるように私はすでに、これらのソリューションを見て... codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF codeproject.com/Articles/342061/Understanding-ASP- NET-Roles-and-Membership-A-Begin codeproject.com/Articles/408306/Understanding-and-Implementing-ASP-NET-Custom-Form
私のコードについてさらに詳しい情報が必要な場合は、ダウンロードすることもできますそれはGitHubから https://github.com/Ruandv/Training/tree/FormsAuthentication
私はあなたの助けに感謝します。
Authorize属性クラスに登録してもらえますか? –
私はカスタムのAuthorize属性クラスを持っていません。私は標準のものを利用しています。 – Gremlin1708