Print Owin Identity Projects

Asked

Viewed 83 times

2

How I can print all different types of claims that I created during my Owin Identity authentication? I’m using @User.Identity.Nome to be able to print only the name, I can’t access the other keys I created.

Currently, I have the following login class:

if (ad.Autentica(Usuario) == true) // SE AUTENTICAR, FAZ O LOGIN
{
    var identity = new ClaimsIdentity(new[]
    {
        new Claim(ClaimTypes.NameIdentifier, Usuario.Login),
        new Claim(ClaimTypes.Name, Usuario.Nome),
        new Claim(ClaimTypes.Surname, Usuario.Sobrenome),
        new Claim(ClaimTypes.Role, Usuario.Departamento),
    }, "SgwCookie");

    Request.GetOwinContext().Authentication.SignIn(identity);

    if (!String.IsNullOrWhiteSpace(model.returnUrl) || Url.IsLocalUrl(model.returnUrl))
    {
        return Redirect(model.returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Dashboard");
    }
}

1 answer

2

You can get all Claims associated with a user with the following code.

if(User.Identity.IsAuthenticated)
{
    //Cria um lista de todas claims do usuario.
    var cliams = ((ClaimsIdentity)User.Identity).Claims.ToList();
}

So you have all user Claims and can make the necessary validations.

Printing in the cshtml

<ul>
@foreach (var claim in ((System.Security.Claims.ClaimsIdentity)User.Identity).Claims)
{
    <li>@claim.Type : @claim.Value</li>

}
</ul>



  • there is no way I can print the value of this Claim directly into . cshtml?

  • Yes it is possible, I will put at the end of the answer

  • Can’t get a specific one without foreach? Just like @User.Identity.Name

  • You can use ((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("claimType").value

  • I haven’t been able to print out the contents of the Claim yet. I solved what I wanted by passing the content I want to session variables directly to the login controller... But I guess it’s not the best way

Browser other questions tagged

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