首页 新闻 会员 周边

asp.net mvc4 身份验证问题无法绕过内置的Membership/Role机制

1
[已解决问题] 解决于 2012-11-20 14:13

平时的项目一般都是mvc3写的,这次使用mvc4,发现用以前的代码没有办法绕过内置的Membership/Role机制

Controller

 [Authorize(Roles = "Admin")]
    public class DefaultController : Controller
    {
        //
        // GET: /Admin/Admin/

        public ActionResult Index()
        {
            return View();
        }

    }

Global.asax

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            //Construst the GeneralPrincipal and FormsIdentity objects
            var authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (null == authCookie)
            {
                //no authentication cokie present
                return;
            }
            var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            if (null == authTicket)
            {
                //could not decrypt cookie
                return;
            }
            //get the role
            var role = authTicket.UserData.Split(new[] { ',' });
            var id = new FormsIdentity(authTicket);
            Context.User = new GenericPrincipal(id, role);
        }

 登录页

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid && _adminBll.ValidateUser(model.UserName, model.Password))
            {
                // 可手动添加FormsAuthenticationTicket
                var ticket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), model.RememberMe, "Admin"); 
                // 加密
                var hashTicket = FormsAuthentication.Encrypt(ticket);
                // 生成cookie 
                var userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
                // 身份验证票Cookie输出到客户端 
                Response.Cookies.Add(userCookie);

                return RedirectToAction("Index", "Default");
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            ModelState.AddModelError("", "提供的用户名或密码不正确。");
            return View(model);
        }

error:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)

这是使用内置的Membership/Role机制 ,因为没有配置好所以报错了。如何能绕过内置的Membership/Role机制 请教了。。以前用这些代码在mvc3里面是没有问题的,可以成功绕过内置的Membership/Role机制

大佬辉的主页 大佬辉 | 菜鸟二级 | 园豆:261
提问于:2012-11-20 10:33
< >
分享
最佳答案
2

把 [Authorize(Roles = "Admin")] 去掉。

奖励园豆:5
Launcher | 高人七级 |园豆:45045 | 2012-11-20 10:57

去掉了怎么做身份验证啊。。现在就是想DefaultController 有admin角色才能浏览。

大佬辉 | 园豆:261 (菜鸟二级) | 2012-11-20 11:05

@大佬辉: 去掉后做验证的方法也有,如果你熟悉Asp.Net的安全机制的话,不去掉的话,你也可以自定义Membership提供程序来替换默认的提供程序来实现。

Launcher | 园豆:45045 (高人七级) | 2012-11-20 11:21

@Launcher: 以前的项目都是用上面的代码来实现自定义Roles的。有例子吗?

大佬辉 | 园豆:261 (菜鸟二级) | 2012-11-20 11:41

@大佬辉: 你那代码不叫自定义MemberShip提供程序。

http://blog.csdn.net/blow_jj/article/details/1519829

Launcher | 园豆:45045 (高人七级) | 2012-11-20 11:46

@Launcher: 嗯。不需要自定义MemberShip,我主要想就是绕过MemberShip/Role机制,用我自定义的。

大佬辉 | 园豆:261 (菜鸟二级) | 2012-11-20 11:49

@大佬辉: 但是最想知道的是为什么用上面的代码mvc3没有问题,mvc4实现不了。

大佬辉 | 园豆:261 (菜鸟二级) | 2012-11-20 11:51

@大佬辉: 如果你的代码没有问题,那么自己看一下AuthorizeAttribute的实现在MVC3和MVC4中的差别。你不需要绕过MemberShip机制,而是自定义 AuthorizeAttribute 来实现自定义授权 。http://blog.csdn.net/cs_victor/article/details/6236991

Launcher | 园豆:45045 (高人七级) | 2012-11-20 14:06

@Launcher: 嗯好的,中午的时候我也是自定义AuthorizeAttribute了。非常感谢。

大佬辉 | 园豆:261 (菜鸟二级) | 2012-11-20 14:11

@Launcher:  thank you ,very much

晃晃悠悠 | 园豆:204 (菜鸟二级) | 2012-12-07 11:14
其他回答(1)
0

provider: SQL Network Interfaces, error: 26解决方法。包括mvc4扩展UserProfile表,非自定义AuthorizeAttribute。

http://www.yn-s.com/News/Details/22

沉默的未知 | 园豆:202 (菜鸟二级) | 2013-03-04 18:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册