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

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è)
国产成人精品亚洲日本片| 男女做爰高清无遮挡免费| 欧美精品一区二区亚洲| 日韩欧美亚洲天堂视频| 日韩毛片资源在线观看| 成人免费视频一区二区三区| 国产精品中文字幕日韩在线| 亚洲av在线视频免费播放| 亚洲精品国产熟女av| 精品人妻中文av一区二区 | 国产欧美日本综合一区| 欧美日韩精品偷拍一区二区| 午夜视频在线观看麻豆| 偷拍盗摄一区二区三区| 日韩成人三级一区二区| 久久亚洲第一视频网站| 亚洲精品一区二区99| 91亚洲国产成人精品性色| 男人一插就想射的原因| 国产日产亚洲综合一区| 人妻少妇系列一区二区| 亚洲成人高清av在线| 免费观看在线视频午夜| 高清国语对白刺激av在线播放| 一区二区三区免费在线观看视频 | av在线观看亚洲天堂| 日本中文字幕免费一区| 国产精品国产精品三级在线观看| 亚洲精品成人综合色在线| 国产黄片一区二区不卡| 成人精品颜射少妇内射| 欧美精品福利一区二区| 日本美女阴部毛茸茸视频| 在线日韩中文字幕二区| 男人的天堂在线观看黄片| 99精品欧美一区二区三区视频| 夫妻在线观看高清视频| 日韩在线不卡播放视频| 黄片无毛欧美在线观看| 国产粉嫩一区二区三区在线观看| 日韩欧美国产麻豆91在线精品|