繁体   English   中英

Aspnet.core 程序登录但考虑同一个表中的数据

[英]The Aspnet.core program logs in but considers the data in the same table

我的代码有问题,程序登录和退出我为每种类型的用户提供三个控制器,当我登录时,程序登录但演示文稿好像它们都来自 Socio 表。 我已经检查了表格并且没有相同的数据,我检查了每种类型用户的视图布局,一切都很好。 有人可以帮我解决这个问题吗?

登录方法在控制器主页中,这是代码:

 public IActionResult Login(string user, string password, string? ReturnUrl)
        {
            SHA512 sha512 = SHA512Managed.Create();
            byte[] bytes = sha512.ComputeHash(Encoding.UTF8.GetBytes(password), 0, Encoding.UTF8.GetByteCount(password));


            string passHash = Convert.ToBase64String(bytes);

            Socios socio = _context.Socios.Include(s => s.Gerir).Include(s => s.Mensagem).Include(s => s.Participa).Include(s => s.PersonalTrainer).Include(s => s.Peso).Include(s => s.PlanosExercicios).FirstOrDefault(s => s.NomeUtilizador == user && s.Password == password);
            if (socio != null)
            {
                HttpContext.Session.SetString("Nome", socio.NomeUtilizador);
                HttpContext.Session.SetInt32("UserId", socio.Idsocio);

                HttpContext.Session.SetString("Perfil", "Socio");

            }
            Professores prof = _context.Professores.Include(p => p.MapaAulasGrupo).Include(p => p.Mensagem).Include(p => p.Peso).Include(s => s.PersonalTrainer).Include(p => p.PlanosExercicios).FirstOrDefault(p => p.Nome == user && p.Password == password);
            if (prof != null)
            {
                HttpContext.Session.SetString("Nome", prof.Nome);
                HttpContext.Session.SetInt32("UserId", prof.Idprofessor);


                HttpContext.Session.SetString("Perfil", "Professor");

            }
            Administrador admin = _context.Administrador.Include(a => a.Gerir).SingleOrDefault(a => a.Nome == user && a.Password == password);
            if (admin != null)
            {
                HttpContext.Session.SetString("Nome", admin.Nome);
                HttpContext.Session.SetInt32("UserId", admin.Idadministrador);


                HttpContext.Session.SetString("Perfil", "Administrador");

            }

            if (String.IsNullOrEmpty(ReturnUrl))
            {
                //ModelState.AddModelError("","Utilizador inexistente na base de dados!");
                //TempData["mensagemErro"] = "Utilizador inexistente na base de dados!";
                //return View("NaoEncontrado","Socios");
                return LocalRedirect("/");
            }
            else
            {
                return LocalRedirect(ReturnUrl);
            }
        }

在布局中我必须区分登录的类型,呈现与每个控制器对应的视图

<header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index"> </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav">
                        @if (SociosController.estaAutenticado(Context))
                        {

                            <li class="nav-item">
                                <a class="nav-link text-white" asp-controller="Socios" asp-action="PerfilSocio">Meu perfil</a>

                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-controller="Socios" asp-action="ListarProfessores">Professores</a>

                            </li>

                            <li class="nav-item">
                                <p class="nav-link text-white">Bem-vindo, @Context.Session.GetString("Nome")</p>
                            </li>

                            <li class="nav-item">
                                <form class="form-inline" asp-controller="Home" asp-action="Logout">
                                    <button type="submit" class="nav-link btn btn-link text-white">Sair</button>
                                </form>
                            </li>
                        }

                        else if (ProfessoresController.estaAutenticado(Context))
                        {
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-controller="Professores" asp-action="Perfil">Meu perfil</a>

                            </li>


                            <li class="nav-item">
                                <p class="nav-link text-white">Bem-vindo, @Context.Session.GetString("Nome")</p>
                            </li>
                            <li class="nav-item">
                                <form class="form-inline" asp-controller="Home" asp-action="Logout">
                                    <button type="submit" class="nav-link btn btn-link text-white">Sair</button>
                                </form>
                            </li>
                        }

                        else if (AdministradoresController.estaAutenticado(Context))
                        {

                            <li class="nav-item">
                                <a class="nav-link text-white" asp-controller="Administradores" asp-action="Perfil">Meu perfil</a>

                            </li>

                            <li class="nav-item">
                                <p class="nav-link text-white">Bem-vindo, @Context.Session.GetString("Nome")</p>
                            </li>

                            <li class="nav-item">
                                <form class="form-inline" asp-controller="Home" asp-action="Logout">
                                    <button type="submit" class="nav-link btn btn-link text-white">Sair</button>
                                </form>
                            </li>

                        }
                        else
                        {
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-controller="Home" asp-action="Login">Entrar </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Registo">Registar</a>
                            </li>
                        }
                    </ul>

                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Index">Pagina Inicial</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Info">Sobre nós</a>
                        </li>


                        <li class="nav-item">
                            <a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="AulasDeGrupo">Aulas de Grupo</a>
                        </li>




                    </ul>
                </div>
            </div>
        </nav>
    </header>

在布局“estaAutenticado”中调用的函数可以在每种类型的用户的控制器中找到,这是代码:

public static bool estaAutenticado(HttpContext contexto)
        {
            if (contexto.Session.GetInt32("UserId") != null)
                return true;
            else
                return false;
        }

有人可以帮我解决这个问题吗? 我不明白为什么他将我登录的所有用户都视为合作伙伴,即使他们来自教师或管理员表。

问题是您为每种类型的用户设置了相同的 Session-key UserId ,您应该进行如下修改:

登录方式

Socios socio = _context.Socios.Include(s => s.Gerir).Include(s => s.Mensagem).Include(s => s.Participa).Include(s => s.PersonalTrainer).Include(s => s.Peso).Include(s => s.PlanosExercicios).FirstOrDefault(s => s.NomeUtilizador == user && s.Password == password);
        if (socio != null)
        {
            HttpContext.Session.SetString("Nome", socio.NomeUtilizador);
            HttpContext.Session.SetInt32("UserId", socio.Idsocio);

            HttpContext.Session.SetString("Perfil", "Socio");

        }
        Professores prof = _context.Professores.Include(p => p.MapaAulasGrupo).Include(p => p.Mensagem).Include(p => p.Peso).Include(s => s.PersonalTrainer).Include(p => p.PlanosExercicios).FirstOrDefault(p => p.Nome == user && p.Password == password);
        if (prof != null)
        {
            HttpContext.Session.SetString("Nome", prof.Nome);
            //set ProfessorId for Professores 
            HttpContext.Session.SetInt32("ProfessorId", prof.Idprofessor);
            HttpContext.Session.SetString("Perfil", "Professor");

        }
        Administrador admin = _context.Administrador.Include(a => a.Gerir).SingleOrDefault(a => a.Nome == user && a.Password == password);
        if (admin != null)
        {
            HttpContext.Session.SetString("Nome", admin.Nome);

            //set AdminId for Administrador 
            HttpContext.Session.SetInt32("AdminId", admin.Idadministrador);
            HttpContext.Session.SetString("Perfil", "Administrador");

        }

每种类型控制器中的 estaAutenticado 方法

//SociosController
public static bool estaAutenticado(HttpContext context)
{
        if (context.Session.GetInt32("UserId") != null)
            return true;
        else
            return false;
}

//ProfessoresController
public static bool estaAutenticado(HttpContext context)
{
        if (context.Session.GetInt32("ProfessorId") != null)
            return true;
        else
            return false;
}

//AdministradoresController
public static bool estaAutenticado(HttpContext context)
{
        if (context.Session.GetInt32("AdminId") != null)
            return true;
        else
            return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM