欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

MVC3----應(yīng)用程序的安全性(1)之Authorize

一、如何禁止匿名訪問

成都創(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)

成都定制網(wǎng)站建設(shè)
伊人婷婷综合激情网| 97国产精品视频在线观看| 国产日韩精品综合一区| 麻豆午夜福利在线播放| 欧美精品一区二区网站| 十八女毛片一区二区三区| 国语对白视频在线观看| 91久久精品凹凸一区二区| 国产亚洲精品视频热| 亚洲人成免费在线观看| 黄色黄色片黄色片黄色| 亚洲成年人黄色在线观看| 日韩商清av毛片网站| 日韩天堂视频在线播放| 热热久久这里只有精品| 99热精品这里只有精品| 国产自拍最新在线视频| 宫部凉花中文字幕在线| 国产亚洲一线二线三线| 五月激情丁香婷婷色网| 亚洲黄色片一区二区三区| 特级艳片在线观看免费| 国产一区二区在线乱码| 特黄日韩免费一区二区三区| 成人高清乱码一区二区三区| 日韩精品欧美中文字幕| 中文岳妇荡欲丰满肥熟| 欧美一区二区精品网站| av中文在线免费观看| 日韩中文不卡人成在线视频| 热久久青草精品欧美一区| 夫妻性生活短是什么原因| 亚洲一区精品二人人爽久久| 日日做日夜夜操天天搞| 91九色国产老熟女乱子| 精品日韩电影在线观看| 国产美女主播视频一区二区三区| 后入视频国产在线观看| 欧美亚洲国产另类第一页| 久久国产精品亚洲看片| 风韵丰满熟妇啪啪老熟女|