Solved

MVC 5 Cannot Successfully Add Claim?

Posted on 2014-11-18
5
270 Views
Last Modified: 2014-12-10
At least if it is added, I cannot return it to a view.
I'm trying to return a User FullName to the ever popular _LoginPartial.cshtml and having difficuties.
I can however return the built-in properties like so:
@using Microsoft.AspNet.Identity

        var identity = (System.Security.Claims.ClaimsPrincipal)System.Threading.Thread.CurrentPrincipal;
        var claims = identity.Claims;
      
         <ul>
            @foreach (var c in claims)
            {                
                <li>@c</li>
            }
        </ul>

Open in new window


I'm trying to add a new Claim at Login like so:
     var user = await UserManager.FindAsync(model.Email, model.Password);

    ... case SignInStatus.Success:

     var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.GivenName, user.FullName)});
     identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FullName));

   ... return RedirectToLocal(returnUrl);  

Open in new window

0
Comment
Question by:WorknHardr
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40452557
I don't understand what you are having a problem with.  Does the identity.Claims list not show the FullName claim?
0
 

Author Comment

by:WorknHardr
ID: 40452710
Correct!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40455244
What does show on the view, in the claims list?
0
 

Accepted Solution

by:
WorknHardr earned 0 total points
ID: 40456160
Well, ahh, the code was changed and there's no backup to looked at. Seems we're using a Session now like so, which works very well...

[_LoginPartial]
@Html.ActionLink("Hello " + @Session["UserDisplayName"] + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })

[AccountController -> Login]
   case SignInStatus.Success:
                    //NEW
                    var user = await UserManager.FindAsync(model.Email, model.Password);
                    string[] fullName = user.FullName.Split(' ');
                    Session["UserDisplayName"] = fullName[0];
                    //END
                    return RedirectToLocal(returnUrl);
    case...

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 40490906
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now