标签:
一:MVC 授权
1. [Authorize] : 可以写在类或者方法上,标记此属性后,必须先登入才可进入此功能
[Authorize] public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); }
2.[Authorize(Users="kitty@163.com")]:用户kitty@163.com可以不用额外登入即可进入此功能;
[Authorize(Users="kitty@163.com")] public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); }
3. [Authorize(Roles="admin")] :只有admin权限的用户才可以使用此功能
[Authorize(Roles="admin")] public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); }
为友好提示,只有admin权限的用户才显示About Menu,在_Layout.cshtml画面上修改代码如下:
<div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> @if (User.IsInRole("admin")) { <li>@Html.ActionLink("About", "About", "Home")</li> } <li>@Html.ActionLink("Contact", "Contact", "Home")</li> </ul> @Html.Partial("_LoginPartial") </div>
标签:
原文地址:http://www.cnblogs.com/kittyguo/p/4629775.html