重写Application_PostAuthenticateRequest方法

本文关键字:方法 PostAuthenticateRequest Application 重写 | 更新日期: 2023-09-27 18:17:20

在连接两个表(Personnes, idstatute personne)时,试图获得字符串中的' titrestatute personne '时无法转换结果的类型

这是方法

    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
        if (FormsAuthentication.CookiesSupported == true)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {
                    //let us take out the username now                
                    string login = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                    string titre = string.Empty;
                    using (AgendaContext db = new AgendaContext())
                    {
                        Personne personne = db.Personnes.SingleOrDefault(p => p.LoginPersonne == login);
                        titre = (from s in db.StatutsPersonne join p in db.Personnes on s.IdStatutPersonne equals p.IdStatutPersonne select new {s.TitreStatutPersonne}).Distinct().ToString();
                    }
                    //let us extract the roles from our own custom cookie

                    //Let us set the Pricipal with our user specific details
                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
                      new System.Security.Principal.GenericIdentity(login, "Forms"), titre.Split(';'));
                }
                catch (Exception)
                {
                    //something went wrong
                }
            }
        }
    } 

重写Application_PostAuthenticateRequest方法

可能你需要更正line:

titre = (from s in db.StatutsPersonne join p in db.Personnes on s.IdStatutPersonne equals p.IdStatutPersonne select s.TitreStatutPersonne).FirstOrDefault();

,因为您试图在new {s处转换为字符串匿名类型对象。TitreStatutPersonne}