vs2010+mvx4 需要安装三个压缩包
Global.asax.cs里面要加的
//验证角色需要导入的命名空间
using System.Web.Security;
using System.Security.Principal;
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<MusicStoreContext>(null);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
//取得当前用户的用户身份标识
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
//取得FormsAuthenticationTicket对象
FormsAuthenticationTicket ticket = id.Ticket;
//取得UserData字段数据(储存的是角色字符串)
string userData = ticket.UserData;
//赋予该用户新的身份(含角色信息)
HttpContext.Current.User = new GenericPrincipal(id, new string[] { userData });
}
}
}
}
}