Keep a user logged into the views

0

I am trying to realize that when I register a user, his username appears in the header of the page. All this in ASP.NET. I have done the validation and I verify that the user really exists, but now I want him to show me his name in the header.

The problem arises that for example the part of the header I have everything in the master page _Layout.cshtml then I do not know how that data could go to that view. My _Layout I have it like this:

<div class="container-fluid" id="header-container">
        <div class="row" id="header">
            <div class="col-12 navbar-personalize" id="menu">
                <div class="container-fluid">
                    <nav class='navbar navbar-expand-lg'>
                        <a class="navbar-brand logo" href="#">NaturaLife</a>
                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse justify-content-around ctmenu" id="navbarNav">
                            <ul class="navbar-nav menu">
                                <li class="nav-item active">
                                    <a class="nav-link" href="/Home/Index">Inicio</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/Home/Productos">Productos</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="#">Recetas</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/Home/AboutUs">¿Quienes Somos?</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="#">Contactanos</a>
                                </li>
                            </ul>
                            <ul class="navbar-nav menu">
                                <li class="nav-item active">
                                    <a class="nav-link" href="/Home/IngresoRegistro"><i class="fas fa-user"></i></a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/Home/Productos"><i class="fas fa-shopping-cart"></i></a>
                                </li>
                                @{

                                }
                                <li class="nav-item">
                                    <a class="nav-link" href="#">NombreUser</a>
                                </li>
                            </ul>
                        </div>
                    </nav>
                </div>
            </div>
            <div class="col-12" id="content-desc">
                <div class="fondo"></div>
                <div class="novedades">
                    <h1>Hola, <span class="ramdom"></span><span class="cursor">|</span></h1>
                </div>
            </div>
        </div>
    </div>

In the part where I have NombreUser is where I want to pass the data and show the name. This header is common for all my pages obviously, so I thought it should go in _Layout.html .

If someone could guide me how to do it, I read with partialView but I'm not sure.

    
asked by Ronald Sanchez 23.05.2018 в 11:43
source

1 answer

0

Use a session variable:

Session["name"] = name;

and to read it in the same class it would be:

name = (string)(Session["name"]);

while in an external class it would be:

HttpContext context = HttpContext.Current;
name = (string)(context.Session["name"]);

You can find more detailed information at the Session documentation

    
answered by 23.05.2018 в 12:13