asp.net-mvc – ASP.Net MVC 4通用主要困难

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – ASP.Net MVC 4通用主要困难前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个ASP.Net MVC 4 Web应用程序。以前,我的MVC应用程序已经使用MVC 3开发,并且使用这个新的MVC 4应用程序,我刚从以前的应用程序复制/重用了我的认证和授权码。

用户登录到我的网站时,我会执行以下操作

帐号控制器

  1. public ActionResult Login(LoginModel model,string returnUrl)
  2. {
  3. if (ModelState.IsValid)
  4. {
  5. User user = _userService.GetUser(model.Email.Trim());
  6.  
  7. //Create Pipe Delimited string to store UserID and Role(s)
  8. var userData = user.ApplicantID.ToString();
  9.  
  10. foreach (var role in user.UserRoles)
  11. {
  12. userData = userData + "|" + role.description;
  13. }
  14.  
  15. _formAuthService.SignIn(user.ApplicantFName,false,userData);
  16.  
  17. return RedirectToAction("Index","Portfolio");
  18. }
  19.  
  20. return View(model);
  21. }

FormsAuthenticationService

  1. public class FormsAuthenticationService : IFormsAuthenticationService
  2. {
  3. public void SignIn(string userName,bool createPersistentCookie,string UserData)
  4. {
  5. if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.","userName");
  6.  
  7. // Create and tuck away the cookie
  8. FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,userName,DateTime.Now,DateTime.Now.AddDays(15),createPersistentCookie,UserData);
  9. // Encrypt the ticket.
  10. string encTicket = FormsAuthentication.Encrypt(authTicket);
  11.  
  12. //// Create the cookie.
  13. HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encTicket);
  14. HttpContext.Current.Response.Cookies.Add(faCookie);
  15. }
  16. }

Global.asax中

  1. protected void Application_AuthenticateRequest(Object sender,EventArgs e)
  2. {
  3.  
  4. // Get the authentication cookie
  5. string cookieName = FormsAuthentication.FormsCookieName;
  6. HttpCookie authCookie = Context.Request.Cookies[cookieName];
  7.  
  8. // If the cookie can't be found,don't issue the ticket
  9. if (authCookie == null) return;
  10.  
  11. // Get the authentication ticket and rebuild the principal
  12. // & identity
  13. FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  14.  
  15. string[] UserData = authTicket.UserData.Split(new Char[] { '|' });
  16.  
  17. GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
  18. GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity,UserData);
  19. Context.User = userPrincipal;
  20.  
  21. }

代码在我以前的MVC 3应用程序中运行良好,但在MVC 4应用程序中,在Razor View中,以下代码似乎没有访问IsInRole属性来执行角色检查

  1. @if (HttpContext.Current.User.IsInRole("Applicant"))
  2. {
  3. <p>text</text>
  4. }

再次,这在我的MVC 3应用程序中完美地工作。

有没有人有任何想法或建议,为什么这不会与我的MVC 4应用程序?

任何帮助深表感谢。

谢谢。

额外信息

我的MVC 4应用程序正在使用.Net Framework 4.0

下面的截图显示了我的General Principal,它被分配给Context.User。您可以看到,对于该用户,m_roles包含两个字符串,即UserID(100170)及其角色(申请人)。但是由于某种原因,IsInRoles不能在我的MVC 4 Razor View中访问或看到,但是它可以在我的MVC 3 Razor View中。

解决方法

乡亲

我终于解决了这个问题。默认情况下,您创建新的ASP.NET MVC 4应用程序时,将启用SimpleMembershipProvider。我不想在这个场合使用SimpleMembershipProvider,但是,我需要在我的web配置中禁用它,具有以下一行

  1. <appSettings>
  2. <add key="enableSimpleMembership" value="false" />
  3. </appSettings>

我给User.IsInRole的电话工作很棒。

希望这有助于别人。

猜你在找的asp.Net相关文章