Get logged in user

5

In my project I have an authentication module and logout . But I wanted it when the user logged in I would take the id from it so that I would display information about the login of it. Type a view of same details. But that the user who logged in had a link and that link already had his id so that when he clicked on the details page he would show the information and if you want to edit you can also get this id .

Namely, I'm using custom authentication. So how can I do this?

I'll put my actions and logout :

   public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Index(String Login, String Senha)
    {
        //verificando login pelo usuario do banco de dados ...
        Usuario login = db.Usuarios.Where(x => x.Login == Login && x.Senha == Senha).FirstOrDefault();
        if (login != null)
        {
            FormsAuthentication.SetAuthCookie(Login, false);
            Session.Add(".PermissionCookie", login.Perfil);
            return RedirectToAction("Index", "Home"); //pagina padrao para todos os usuarios...
        }
        return RedirectToAction("Index");
    }
    public ActionResult Sair()
    {

        FormsAuthentication.SignOut();
        Session.Remove(".PermissionCookie");
        return RedirectToAction("Index");
    }       
    
asked by anonymous 18.11.2014 / 14:09

3 answers

5

@DiegoZanardo's answer is very close to the request in question. I'll just add some information:

SetAuthCookie

I think it's wrong to put an ID field in a Cookie that accepts String , so it would change to that:

FormsAuthentication.SetAuthCookie(login.Login.toString(), false);

Controller , not View

public ActionResult Detalhes()
{
    if (User.Identity.IsAuthenticated)
    {
        var userName = User.Identity.Name;
        var usuario = db.Usuarios.FirstOrDefault(x => x.Login == userName);

        if (usuario != null)
            return View(usuario);
    }

    return RedirectToAction("Index");
}

View

@model Usuario

<p>Login: @usuario.Login</p>
<p>Data de Nascimento: @usuario.DataNascimento</p>
@* Coloque aqui mais detalhes da sua View *@
    
18.11.2014 / 19:55
4

First, FormsAuthentication.SetAuthCookie gets a String and a Boolean , SetAuthCookie(String, Boolean) . Not being SetAuthCookie(Usuario, Boolean) , how are you doing?

So it would look like this, assuming that the class Login has ID :

FormsAuthentication.SetAuthCookie(login.ID.toString(), false);

Then your View details would be:

public ActionResult Detalhes()
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        int ID = int.Parse(HttpContext.Current.User.Identity.Name);
        Usuario usuario = db.Usuarios.Where(x => x.ID == ID).FirstOrDefault();
        return View(usuario);
    }
    return RedirectToAction("Index");
}
    
18.11.2014 / 14:25
1

Yes, using the .GetUserId () method

For the method to be directly available, you need to use:

using Microsoft.AspNet.Identity;

After this you can get the id of the logged in user:

User.Identity.GetUserId();
    
12.01.2017 / 14:32