アプリで認証/承認を処理するための最良の方法を正確に把握しようと、しばらく時間を費やしてきました。Windows 認証を使用しており、ユーザー名を読み取ることができます。
ユーザーを承認するには、データベースにクエリを実行してロールを取得し、そのロールを持つユーザーのカスタム プリンシパルを作成します。
以下にあるものはうまくいくと思いますが、セッション変数をチェックとして使用する代わりに、データベースを1回だけクエリするより良い方法があるかどうかに興味がありましたか?
以下のコードは、私の Global.asax にあります。
protected void Application_AuthenticateRequest(object sender, EventArgs args)
{
if (HttpContext.Current != null)
{
if (this.Session["Authenticated"] == null)
{
using (AppToolsEntities db = new AppToolsEntities())
{
var user = db.ADObjects.Where(x => x.CN == User.Identity.Name.RemoveDomain()).FirstOrDefault();
string[] roles = new string[] { user.Title == "EA" ? "Reviewer" : "Admin" };
GenericPrincipal principal = new GenericPrincipal(HttpContext.Current.User.Identity, roles);
Thread.CurrentPrincipal = HttpContext.Current.User = principal;
this.Session["Authenticated"] = true;
}
}
}
}
私はこれを正しい方法で行っていますか?
前もって感謝します。