Get logged in user

Asked

Viewed 6,557 times

5

In my project I have an authentication module and logout. But I wanted when the user logged in I’d get the id of it so that I would show the information concerning the login of it. Like a view details. But that the user who logged in, had a link and in that link had the id so that when he clicked on the details page showed the information and if you want to edit it is also possible to take this id.

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

I’ll put my actions of login 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");
    }       

3 answers

6


The reply of @Diegozanardo gets very close to the request in question. I will just add some information:

SetAuthCookie

I think it’s wrong to put an ID field on a Cookie who accepts String, then I’d change to this:

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

Controller, nay 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 *@
  • Gypsy how can I put this user id to a link so I can redirect the user to a detail page and edit the data ? Type the Manager of Identity. This link would be on partial that would be rendered in _Layout. Leaving so that ta ai that you did, appearing the user name.

  • @Érikthiago How do you want the link format? This influences how the Controller call will be built.

  • Type, I wanted the link to show the name of the user and when clicking it was redirected to a page that has the details related to it and this page has a link to edit its data as well.

  • My answer and @Diegozanardo’s answer already do that. What would be missing from them?

  • But what happens is that the Id user this way you passed me... How would I do ?

  • You can take the user ID internally. There’s no reason for it to come in the URL or any other device.

  • How would I do that then ? Because I wanted the link to recognize the user id so that it could change its data and see the details too

  • The user is yours or someone else’s?

  • The user who is logged in to the application

  • As I told you, and the answers show well, don’t need. You just need to make use of the context and bring the user identification in-house, using User.Identity.Name.

Show 5 more comments

4

First place, the FormsAuthentication.SetAuthCookie receives a String and a Boolean, SetAuthCookie(String, Boolean). Cannot be SetAuthCookie(Usuario, Boolean), how are you doing.

Soon it would look like this, assuming the class Login has been ID:

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

So your View of 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");
}
  • that Current is not recognized, gives error... Has some notation that is used, or some using ? Because he’s not recognized.

  • And how do I call this action from the _Layout ? So that when clicking is redirected to the details concerning that id you are logged ?

  • @Érikthiago @Diegozanardo No need to use HttpContext.Current.User. Only User the Controller already recognizes.

  • @Ciganomorrisonmendez could put in response then ? so that I can put on the link the id and such ?

1

Yes, using the method .Getuserid()

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

using Microsoft.AspNet.Identity;

After that you can get the login user id:

User.Identity.GetUserId();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.