無法將類型為“System.Security.Principal.GenericIdentity”的對象強制轉換為類型“System.Web.Security.FormsIdentity”。

來源:互聯網
上載者:User

標籤:

 

     這次的系統包含兩個子系統,所以做了一個分角色的身分識別驗證系統。

web.config <system.web>中設定

<authentication mode="Forms">     <!--驗證方式為表單驗證-->      <forms name=".ASPXAUTH" loginUrl="~/login.aspx" protection="All" timeout="60" path="/"></forms>    </authentication>    <authorization>      <allow users="?"/>    <!--根目錄網頁預設為所有使用者都能訪問-->      </authorization>

外面添加

<!--設定admin目錄的存取權限-->    <location path="admin/Default.aspx">    <system.web>      <authorization>        <!--允許角色為admin的使用者可以訪問-->        <allow roles="admin"/>       <!--拒絕所有其他使用者訪問-->        <deny users="*"/>       <!--注意以上兩句的次序不能變-->      </authorization>    </system.web>  </location>    <!--設定index目錄的存取權限-->  <location path="index/Default.aspx">    <system.web>      <authorization>     <!--允許角色為stu的使用者可以訪問-->        <allow roles="stu"/>      <!--拒絕所有其他使用者訪問-->        <deny users="*"/>       <!--注意以上兩句的次序不能變-->      </authorization>    </system.web>  </location>

 

 

 

login.aspx頁面身分識別驗證通過後

   //建立一個身分識別驗證票                  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, UserRole);                //將身分識別驗證票加密                  string EncrTicket = FormsAuthentication.Encrypt(ticket);                //建立一個Cookie                  HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName, EncrTicket);                //將Cookie寫入用戶端                  Response.Cookies.Add(myCookie);                //跳轉到初始請求頁或預設頁面                  Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, false));

golbal全域應用程式中添加代碼

 void Application_AuthenticateRequest(object sender, EventArgs e)    {        //判斷正在請求頁的使用者的身分識別驗證資訊是否為空白          if (HttpContext.Current.User != null)        {            //判斷使用者是否已經進行了身分識別驗證              if (HttpContext.Current.User.Identity.IsAuthenticated)            {                //判斷目前使用者身分識別驗證的方式是否為Forms身分識別驗證方式                  if (HttpContext.Current.User.Identity is FormsIdentity)                {                    //獲得進行了Forms身分識別驗證的使用者標識                      FormsIdentity UserIdent = (FormsIdentity)(HttpContext.Current.User.Identity);                    //從身分識別驗證票中獲得使用者資料                      FormsAuthenticationTicket ticket = UserIdent.Ticket;                    string UserData = ticket.UserData;                    //string UserData = UserIdent.Ticket.UserData;                    //分割使用者資料得到使用者角色數組                      string[] rolues = UserData.Split(‘,‘);                    //從使用者標識和角色群組初始化GenericPrincipal類                      HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(UserIdent, rolues);                }            }        }    }  

身分識別驗證設定完成後設定頁面跳轉

default頁面添加代碼

 FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;                if (id.Ticket.UserData.Contains("admin"))                {                    //跳轉到存取權限不夠的錯誤提示頁面                    Response.Redirect("~/admin/Default.aspx", true);                }                else if (id.Ticket.UserData.Contains("stu"))                {                    Response.Redirect("~/index/Default.aspx", true);                }                else                    Response.Redirect("~/login.aspx", true);

至此整個身分識別驗證完成。

vs上運行沒有問題,發布到IIS後報錯無法將類型為“System.Security.Principal.GenericIdentity”的對象強制轉換為類型“System.Web.Security.FormsIdentity”。

尋找資料:

1、將網站應用程式程式池的託管管道模式由“整合”改為“經典”

2、網站“處理常式映射”添加”.aspx“請求路徑的可執行檔“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll”,或者將ISAPI中4.0的狀態改為可用狀態

以上網友成功解決問題的辦法在我這都不行。。。。。

login.aspx,golbal.asax,default.aspx,代碼調整過無數次,各種方法試過都無效。

 ===================================================================================================================

突發奇想將登陸ip改為  192.127.1.8080/login.aspx,。。。OK,終於不報錯誤介面了。

錯誤資訊提示 無法將類型為“System.Security.Principal.GenericIdentity”的對象強制轉換為類型“System.Web.Security.FormsIdentity”,身分識別驗證出了問題,一直以為是iis對角色身分識別驗證有什麼特殊設定,但最後發現時預設文件沒有設定正確。

使用“預設文件”功能頁可配置預設文件的列表。如果使用者訪問您的網站或應用程式,但沒有指定文檔名),則可以配置 IIS 提供一個預設文件,如 Default.htm。IIS 將返回與目錄中的檔案名稱匹配的列表中的第一個預設文件。

我的登陸頁面是login.aspx,IIS搜尋返回default.aspx。default.aspx頁面中有一行代碼

 FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
(FormsIdentity)HttpContext.Current.User.Identity;中的formsIdentity表示“一個使用了forms身分識別驗證進行了身分識別驗證的使用者標識”,192.127.1.8080請求發送跳過了身分識別驗證介面,所以報錯。
解決:在預設文件中“添加”login.aspx


(每次網站發布後都需要添加login預設文件,所以想在代碼中定位到login頁面去,但發現無論是在default頁面進行設定跳轉,還是在golbal頁面添加設定,都會報錯,必須添加login頁面首先進行身分識別驗證
)

 

無法將類型為“System.Security.Principal.GenericIdentity”的對象強制轉換為類型“System.Web.Security.FormsIdentity”。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.