重写Application_PostAuthenticateRequest方法

时间:2015-10-12 12:30:44

标签: c# authentication model-view-controller methods override

在连接两个表(Personnes,IdStatutPersonne)时尝试获取字符串中的'TitreStatutPersonne'时,无法转换结果的类型

这是方法

    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
                }
            }
        }
    } 

1 个答案:

答案 0 :(得分:0)

可能你需要更正一行:

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

因为您尝试在新的{s.TitreStatutPersonne}转换为字符串anonimous类型对象