0

I followed this tutorial to get my live account oauth working on my asp mvc 5 and web api 2 app.

All goes well and with an additional scope I can fetch the users e-mail. But how do I pass it through back to the webpage.

Some code:

In Startup.Auth.cs

        Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationOptions microsoftAccountOptions = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationOptions(){
            ClientId = microsoftClientId,
            ClientSecret = microsoftClientSecret
        };

        microsoftAccountOptions.Scope.Add("wl.basic");
        microsoftAccountOptions.Scope.Add("wl.emails");


        microsoftAccountOptions.Provider = new Microsoft.Owin.Security.MicrosoftAccount.MicrosoftAccountAuthenticationProvider()
        {
            OnAuthenticated = async context =>
            {
                context.Identity.AddClaim(new System.Security.Claims.Claim("urn:microsoftaccount:access_token", context.AccessToken));

                context.Identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Email, context.User["emails"].Value<string>("account")));

            }
        };


        app.UseMicrosoftAccountAuthentication(microsoftAccountOptions);

So in the OnAuthenticate call back I can add the e-mail to the claims. This all goes wel but when I try to access it in my MVC controller (the redirect url) all the claims are gone. I'm using Web Api and MVC so it's not that strange that those claims are gone. Since the AccountController is an ApiController and the redirect to the HomeController is most likely a different request.

    public ActionResult Index()
    {

        var claims = HttpContext.GetOwinContext().Authentication.User.Claims;

        ViewBag.Title = "Home Page";

        return View(claims);
    }

How can I pass through the e-mail claim?

user1613512
  • 3,590
  • 8
  • 28
  • 32

1 Answers1

0

The ClaimsPrincipal should be accessible on the HttpContext here:

var claims = (ClaimsPrincipal)HttpContext.Current.User;
John Mc
  • 2,862
  • 1
  • 22
  • 37