一、如何禁止匿名訪問
成都創(chuàng)新互聯(lián)成立與2013年,先為扎囊等服務(wù)建站,扎囊等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為扎囊企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1,配置Web.config文件
<authentication mode="Forms"> <forms loginUrl="~/Account/ZhangDi" timeout="2880" /> </authentication>
2,控制器代碼
①:阻止匿名訪問:
[Authorize]
public ActionResult Edit(int id)
{
PersonError person = db.PersonErrors.Single(r => r.STU_ID == id);
return View(person);
}
②:還可以阻止匿名訪問整個控制器:
[Authorize]
public class PersonErrorController : Controller
{
}
③:創(chuàng)建一個身份驗(yàn)證票證(有了這個就可以訪問頁面了)
returnUrl:返回的路徑
[HttpPost]
public ActionResult ZhangDi(string txtname, string returnUrl)
{
//第二個參數(shù)為true會記住密碼
FormsAuthentication.SetAuthCookie(txtname, false);
//判斷是否是有效的路徑
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("http://") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
④:獲取用戶名稱
//是否驗(yàn)證了用戶
if(User.Identity.IsAuthenticated)
{
//獲取用戶名稱
string username = User.Identity.Name;
}
⑤:刪除身份驗(yàn)證票證
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
二、對特定角色訪問權(quán)限控制
1,配置Web.config文件
<authentication mode="Forms"> <forms loginUrl="~/Account/ZhangDi" timeout="2880" /> </authentication>
2,多種授權(quán)方式
①:對多個角色授權(quán)訪問:
[Authorize(Roles="admin,superadmin")]
public class PersonErrorController : Controller
{
}
②:對多個用戶授權(quán)訪問:
[Authorize(Users="test1,test2")]
public class PersonErrorController : Controller
{
}
③:同時授權(quán)給用戶和角色
[Authorize(Roles="admin,user",Users="test1,test2")]
public class PersonErrorController : Controller
{
}
3,控制器代碼
①:在Global.asax配置Application_AuthenticateRequest事件(當(dāng)安全模塊已建立用戶標(biāo)識時發(fā)生)
protected void Application_AuthenticateRequest() { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { FormsIdentity userident = (FormsIdentity)HttpContext.Current.User.Identity; string UserData = userident.Ticket.UserData; string[] roles = UserData.Split(','); //設(shè)置用戶角色 HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(userident, roles); } } } }
②:創(chuàng)建一個身份驗(yàn)證票證
[HttpPost] public ActionResult ZhangDi(string txtname, string returnUrl) { FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1,//票證的版本號 txtname,//用戶名 DateTime.Now,//票證發(fā)出的時間 DateTime.Now.AddHours(1),//票證過期時間 false , //是否將票證持久存儲在cookie中(和記住密碼沒關(guān)系) "admin");//角色 //加密驗(yàn)證票證 string ticketEncrypt = FormsAuthentication.Encrypt(ticket); //實(shí)例化cookie HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypt); //記住密碼 cookie.Expires = DateTime.MaxValue; cookie.Path = "/"; //將cookie添加到cookie集中 Response.Cookies.Add(cookie); return Redirect(returnUrl); }
③:獲取用戶名稱
//是否驗(yàn)證了用戶
if(User.Identity.IsAuthenticated)
{
//獲取用戶名稱
string username = User.Identity.Name;
}
④:刪除身份驗(yàn)證票證
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
本文名稱:MVC3----應(yīng)用程序的安全性(1)之Authorize
地址分享:http://www.aaarwkj.com/article26/iijhjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、小程序開發(fā)、網(wǎng)站導(dǎo)航、用戶體驗(yàn)、域名注冊、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)