How to pass parameters to the _Layout.cshtml view in Asp.Net projects

0
Hello, seeing that I have a% control panel%, my layout basically changes only the center with the Area , however I want it for features in the views part, and for those functions I need access the object of the person logged in. How will I do this?

    
asked by anonymous 17.05.2014 / 18:52

1 answer

2

In my projects I usually use a class static called UsuarioRepositorio As below:

public class UsuarioRepositorio
    {
        public static bool AutenticarUsuario(string login, string senha)
        {
            var context = new NewsContext();

            var user = context.Users.SingleOrDefault(u => u.Login == login && u.Status);

            if (user == null)
            {
                return false;
            }
            if (!Crypto.VerifyHashedPassword(user.Senha, senha))
            {
                return false;
            }

            FormsAuthentication.SetAuthCookie(user.Login, false);

            return true;
        }

        public static User GetLogedUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) return null;

            var login = HttpContext.Current.User.Identity.Name;

            var context = new NewsContext();

            if (string.IsNullOrEmpty(login))
            {
                return null;
            }

            var user = context.Users.SingleOrDefault(u => u.Login == login && u.Status);
            return user;
        }

        public static void LogOf()
        {
            FormsAuthentication.SignOut();
        }
    }

Where I have 3 methods, AutenticarUsuario , GetLogedUser and LogOf .

When I need to do some action I need to get the information of the logged in user, for example: Print the name of the User logged in to the Administrative Panel.

I use this way:

@using Projeto.Repositorio

<label>Bem vindo: @UsuarioRepositorio.GetLogedUser().Nome</label>

It works perfectly for me.

And you can use it in other ways, such as checking if the user has some property or Role that defines it as Admin and only print the menu items allowed for the rule where it is located.

Follow link of the article where I learned to use these methods.

    
17.05.2014 / 23:17