Configuring MVC app using Auth0 for ClaimsIdentity and nameidentifier error msgs

854 views Asked by At

I'm using the Auth0-Mvc NuGet package (latest 0.9.1) with MVC 4 in VS 2015. Targeting .NET 4.5.2

When specifying a @Html.AntiForgeryToken() in a partial view, I encounter a

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.

Using other advice for similar problem, I modify Global.asax.cs to include:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}

This leads to:

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity.

How can I modify my code to solve this ClaimsIdentity and nameidentifier issue?

I've checked the MVC 4 sample encounters an unresolvable runtime error, and its source doesn't have any modifications found out in the community. It does have @Html.AntiForgeryToken(), so I'm hoping there's a solution.

My CallbackHandler.ashx is same as delivered in the NuGet package 0.9.1

public void ProcessRequest(HttpContext context)
    {
        var token = client.ExchangeAuthorizationCodePerAccessToken(context.Request.QueryString["code"], ConfigurationManager.AppSettings["auth0:CallbackUrl"]);
        var profile = client.GetUserInfo(token.AccessToken);

        var user = new Dictionary<string, string>
        {
            { "name", profile.Name??"" },
            { "email", profile.Email??"" },
            { "family_name", profile.FamilyName??"" },
            { "given_name", profile.GivenName??"" },
            { "gender", profile.Gender??"" },
            { "nickname", profile.Nickname??"" },
            { "picture", profile.Picture??"" },
            { "user_id", profile.UserId??"" },
            { "id_token", token.IdToken }
        };

        ClaimsCookie.ClaimsCookieModule.Instance.CreateSessionSecurityToken(user);

        var claimedUser = new User
        {
            AccessToken = user["id_token"],
            UserID = user["user_id"],
            Name = user["name"],
            Email = user["email"],
            NickName = user["nickname"],
            ProfilePicUrl = user["picture"]
        };

        //go save to database.
        //then redirect to another URL
0

There are 0 answers