0

I have installed the IS3/MR/IDM combination and everything is working fine. What I need to do now is make the the logged in user (ID, Name etc) available to all my MVC controllers so the obvious choice is to create a base controller so all others controllers inherit from it.

Could anyone advise if this is the best way to achieve this and perhaps provide some sample code?

Tim
  • 81
  • 3
  • 11
  • What stack are you on? What do you need to accomplish with the user? Assuming more recent .NET, there is already a User object on the MVC controller that can be casted to a `ClaimsPrincipal`. – beavel Nov 12 '16 at 17:29

1 Answers1

0

Assuming you are already successfully authenticating against Identity Server 3, you should be all set already. If you look in the CallApiController you'll find this method

        // GET: CallApi/UserCredentials
    public async Task<ActionResult> UserCredentials()
    {
        var user = User as ClaimsPrincipal;
        var token = user.FindFirst("access_token").Value;
        var result = await CallApi(token);

        ViewBag.Json = result;
        return View("ShowApiResult");
    }

the user variable should already contain claims for the user's name, Id and such. So

var id = user.FindFirst(Constants.ClaimTypes.Subject).Value;

var firstName = user.FindFirst(Constants.ClaimTypes.GivenName).Value;

var middleName = user.FindFirst(Constants.ClaimTypes.MiddleName).Value;

var lastName = user.FindFirst(Constants.ClaimTypes.LastName).Value;

Of course, that all assumes that you've got that information in your store of user information and I'm not checking for the errors that will occur if they are not there.

GlennSills
  • 3,977
  • 26
  • 28