I have the MVC application, using the Owin and Asp.Net Identity, and using the useCookieauthentication

During login process, I have added the custom claim, and I gets properly sign-in. ```

    [HttpPost]
    public async Task<ActionResult> Login(LoginViewModel loginViewModel)
    {
        var user = UserManager.FindByName(loginViewModel.UserName);
        var signInStatus = UserManager.CheckPassword(user, loginViewModel.Password);

        if (signInStatus)
        {
            user.Claims.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim() { ClaimType = "CustomClaim", ClaimValue = loginViewModel.UserName.Trim() });
            SignInManager.SignIn(user, true, false);
            return Redirect(url);
        }
        else
        {
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(loginViewModel);
        }
    }

When I get the callback to my one of Action method, I try to retrieve Claim Custom Claim that I have stored during login process. Locally when I run and debug this code it works correctly as expected. But when I deploy the application to azure I am unable to get the custom claim value.

       public ActionResult Index()
        {
            var claimsPrincipal = System.Web.HttpContext.Current.User as System.Security.Claims.ClaimsPrincipal;
            var customClaimValue = claimsPrincipal.Identities.First().Claims.First(x => x.Type.Equals("CustomClaim")).Value;
            return View();
        }
1

There are 1 best solutions below

0
Mohit Verma On

Couple of things to try, Let me know if it still doesn't work for you, Posting as per my recent experience which i covered in my other answer.

Also please troubleshoot further to understand more on the inner stack details.

  • As @Joey Cai mentioned in his answer ,Change your **Action to take when request is not authenticated in App Service** Authentication/Authorization section in the azure portal from LogIn with Azure Active Directory to **Allow Anonymous requests**. As shown on the picture below:

enter image description here

  • If above option doesn't work out try below:

    Try changing the application manifest of the application definition on Azure to set the "oauth2AllowIdTokenImplicitFlow" property to true from false.

    • Go to the Azure Portal,
    • Select to Azure Active Directory
    • Select App Registrations
    • Select your app.
    • Click on Manifest
    • Find the value oauth2AllowIdTokenImplicitFlow and change it's value to true
    • Click Save

Asp.net UseOpenIdConnectAuthentication not working in Azure

Hope it helps.