ASP.Net MVC4Webアプリケーションを開発しています。以前、私のMVCアプリケーションはMVC 3を使用して開発されていましたが、この新しいMVC 4アプリケーションでは、以前のアプリケーションから認証および認証コードをコピー/再利用しました。
ユーザーが私のサイトにログインすると、次のようになります
アカウントコントローラー
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
User user = _userService.GetUser(model.Email.Trim());
//Create Pipe Delimited string to store UserID and Role(s)
var userData = user.ApplicantID.ToString();
foreach (var role in user.UserRoles)
{
userData = userData + "|" + role.description;
}
_formAuthService.SignIn(user.ApplicantFName, false, userData);
return RedirectToAction("Index", "Portfolio");
}
return View(model);
}
FormsAuthenticationService
public class FormsAuthenticationService : IFormsAuthenticationService
{
public void SignIn(string userName, bool createPersistentCookie, string UserData)
{
if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
// Create and tuck away the cookie
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(15), createPersistentCookie, UserData);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(authTicket);
//// Create the cookie.
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
HttpContext.Current.Response.Cookies.Add(faCookie);
}
}
Global.asax
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// Get the authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
// If the cookie can't be found, don't issue the ticket
if (authCookie == null) return;
// Get the authentication ticket and rebuild the principal
// & identity
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
string[] UserData = authTicket.UserData.Split(new Char[] { '|' });
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity, UserData);
Context.User = userPrincipal;
}
このコードは以前のMVC3アプリケーションではうまく機能しますが、このMVC 4アプリケーションでは、Razorビュー内で、次のコードがIsInRoleプロパティにアクセスして役割チェックを実行していないようです。
@if (HttpContext.Current.User.IsInRole("Applicant"))
{
<p>text</text>
}
繰り返しますが、これは私のMVC3アプリケーションで完全に機能しました。
これが私のMVC4アプリケーションで機能しない理由について誰かがアイデアや提案を持っていますか?
どんな助けでも大歓迎です。
ありがとう。
追加情報
私のMVC4アプリケーションは.NetFramework4.0を使用しています
以下のスクリーンショットは、 Context.Userに割り当てられている私のジェネリックプリンシパルを示しています。このユーザーの場合、m_rolesにはUserID(100170)とそのRole (Applicant)の2つの文字列が含まれていることがわかります。しかし、何らかの理由で、IsInRolesにアクセスしたりMVC 4 Razor Viewで表示したりすることはできませんが、同じMVC 3RazorViewで表示することはできます。