tco*_*ode 17 asp.net-mvc authorization razor asp.net-mvc-4
我正在开发一个ASP.Net MVC 4 Web应用程序.以前我的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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
这段代码在我以前的MVC 3应用程序中运行良好,但是在这个MVC 4应用程序中,在Razor View中,以下代码似乎没有访问IsInRole属性来执行角色检查
@if (HttpContext.Current.User.IsInRole("Applicant"))
{
<p>text</text>
}
Run Code Online (Sandbox Code Playgroud)
同样,这在我的MVC 3应用程序中完美运行.
有没有人有任何想法或建议,为什么这不适用于我的MVC 4应用程序?
任何帮助深表感谢.
谢谢.
额外信息
我的MVC 4应用程序正在使用.Net Framework 4.0
下面的屏幕截图显示了我的Generic Principal,它被分配给Context.User.您可以看到,对于此用户,m_roles包含两个字符串,UserID(100170)和他们的角色(申请人).但由于某些原因,在我的MVC 4 Razor View中无法访问或看到IsInRoles,但是,它可以在我相同的MVC 3 Razor View中.

tco*_*ode 18
乡亲
我终于解决了这个问题.默认情况下,在创建新的ASP.NET MVC 4应用程序时会启用SimpleMembershipProvider.我不想在这种情况下使用SimpleMembershipProvider,但是,我需要在我的web配置中使用以下行禁用它
<appSettings>
<add key="enableSimpleMembership" value="false" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我对User.IsInRole的调用现在很有用.
希望这有助于其他人.
| 归档时间: |
|
| 查看次数: |
7741 次 |
| 最近记录: |