私は認証と承認についてオンラインでたくさんのことを読んでいて、最終的に機能しているように見えるいくつかのコードに落ち着きました...しかし、それが行っていることすべてを完全には理解していません(FormsAuthenticationTicketまで)。
私が使用している MVC アプリは、いくつかの機密データを処理する予定であり、認証と承認に関連するすべてのことをトリプル チェックしたいと考えています。
Windows 認証を使用しています。
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
追加のユーザーおよび権限情報を含む一連のテーブルが SQL Server にあります。
- ユーザー
- 役割
- ユーザーの役割
- アプリケーション内のオブジェクトと関連する権限を定義するその他のテーブル。
私の Global.asax には、からインスピレーションを得た次のメソッドがあります。
http://www.codeproject.com/Articles/5182/Insight-into-Security-Model-using-Principal-and-Id
protected void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
if (e.Identity == null || !e.Identity.IsAuthenticated)
{
//Redirect to error or access denied page
}
string userData = e.Identity.AuthenticationType;
var cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;
if (cachedUser == null)
{
var user = repo.GetUserByFullUserName(e.Identity.Name);
HttpContext.Current.Cache.Insert(e.Identity.Name, user, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;
}
var userIdentity = e.Identity.Name;
var formsAuthTicket = new FormsAuthenticationTicket(1, e.Identity.Name, DateTime.Now, DateTime.Now.AddMinutes(2), false, userData);
var encryptedTicket = FormsAuthentication.Encrypt(formsAuthTicket);
var httpcook = new HttpCookie("authCookie", encryptedTicket);
Response.Cookies.Add(httpcook);
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
var httpcook = Context.Request.Cookies["authCookie"];
var formsAuthTicket = FormsAuthentication.Decrypt(httpcook.Value);
var cachedUser = GetCachedUser(formsAuthTicket.Name);
if (cachedUser == null)
{
cachedUser = CreateCachedUser(formsAuthTicket.Name);
}
var genIdentity = new GenericCustomIdentity(cachedUser, Request.IsAuthenticated, formsAuthTicket.UserData);
var genPrincipal = new GenericCustomPrincipal(genIdentity, cachedUser);
HttpContext.Current.User = genPrincipal;
}
}
だからここに私の質問があります:
WindowsAuthentication_OnAuthenticate メソッドに FormsAuthenticationTicket があるのはなぜですか? WinAuth メソッドで Identity オブジェクトと Principal オブジェクトを構築することはできませんか?
ユーザー データを HttpContext.Current.Cache に格納することはセキュリティ リスクですか? これらのメソッドは何度も呼び出されるため、リクエストごとにデータベースにアクセスしたくありません。より良い/より安全な代替手段はありますか?
FormsAuthenticationTickets、Identity、および Principal の使用に慣れていないので、コメントや提案をいただければ幸いです。申し訳ありませんが、これは質問ではありませんでした。