locked
Owin authentication middleware-Context data not available in different controller RRS feed

  • Question

  • User1720150233 posted
    Remove alert | Edit | Delete | Change type
    Question
    You cannot vote on your own post
    0
    1. Validation is happening in different system and passing the userId from parent application

    if(IsAuthorizeUser(txtUsername.Text))

    {

    FormsAuthentication.RedirectFromLoginPage(txtUsername.Text.ToUpper() + ','+ FullName, true);

    Response.Redirect(redirectURL, false);

    }

    2. to this application. Where I a capturing the passed user id from HttpContext

    [ActionName("Index"), HttpGet]

    publicasyncTask<ActionResult> Index(stringreturnUrl)

    {

    //return View(new SignInViewModel());

    try

    {

    //Check the role. set the user context/custom claims

    //Redirect to the corresponding controller.

    loggedInUserID = HttpContext?.User?.Identity?.Name?.Split(',')[0];

    varresult = awaitthis._signinmanager.PasswordSignInAsync(loggedInUserID, "testpwd", true, shouldLockout: false);

    //StartTemp code Check the claims data in context

    ClaimsPrincipal principal = System.Web.HttpContext.Current.User asClaimsPrincipal;

    stringrole = GetClaimValue(principal, ClaimTypes.Role);

    stringCompanyId = GetClaimValue(principal, MyClaimTypes.CompanyId);

    stringSuperUser = GetClaimValue(principal, MyClaimTypes.SuperUser);

    //End Temp code Check the claims data in context

    switch(result)

    {

    caseSignInStatus.Success:

    returnRedirectToAction("Index", "Documents");

    default:

    returnRedirect(WebConfigurationManager.AppSettings["LogoutUrl"]);

    }

    }

    catch(Exception ex)

    {

    returnRedirect(WebConfigurationManager.AppSettings["LogoutUrl"]);

    }

    }

    3. Adding custom claims to access later from different controller

    public class ApplicationUser : IdentityUser

    {

    publicApplicationUser()

    { }

    publicstringUserId { get; set; }

    publicstringCompanyCode { get; set; }

    publicDateTimeOffset? LockoutEnd { get; set; }

    publicasyncTask<ClaimsIdentity> GenerateUserIdentityAsync(MyUserManager manager, stringauthenticationType)

    {

    ClaimsPrincipal principal = System.Web.HttpContext.Current.User asClaimsPrincipal;

    AddUpdateClaim(principal, ClaimTypes.Role, "userRole");

    AddUpdateClaim(principal, MyClaimTypes.CompanyId, "CompanyId");

    AddUpdateClaim(principal, MyClaimTypes.SuperUser, "userRole");

    returnprincipal.Identity asClaimsIdentity;

    }

    // add new claim

    identity.AddClaim(newClaim(key, value));

    varauthenticationManager = HttpContext.Current.GetOwinContext().Authentication;

    authenticationManager.AuthenticationResponseGrant = newAuthenticationResponseGrant(newClaimsPrincipal(identity), newAuthenticationProperties() { IsPersistent = true});

    }

    }

    //Code snippet for AddUpdateClaim method.

    privatevoidAddUpdateClaim(IPrincipal currentPrincipal, stringkey, stringvalue)

    {

    varidentity = currentPrincipal.Identity asClaimsIdentity;

    if(identity == null)

    return;

    // check for existing claim and remove it

    varexistingClaim = identity.FindFirst(key);

    if(existingClaim != null)

    identity.RemoveClaim(existingClaim);


    4. I am able to get those value in home controller successfully

    //StartTemp code Check the claims data in context

    ClaimsPrincipal principal = System.Web.HttpContext.Current.User asClaimsPrincipal;

    stringrole = GetClaimValue(principal, ClaimTypes.Role);

    stringCompanyId = GetClaimValue(principal, MyClaimTypes.CompanyId);

    stringSuperUser = GetClaimValue(principal, MyClaimTypes.SuperUser);

    //End Temp code Check the claims data in context


    5. After redirection to Documents controller derived from SecureController(publicclassDocumentsController: SecureController) I am not getting those claims value getting null value

    publicSecureController()

    {

    this._viewModelError = newViewModelError();

    HttpContext context = System.Web.HttpContext.Current;

    if(context.User.Identity.IsAuthenticated)

    {

    //StartTemp code Check the claims data in context

    ClaimsPrincipal principal = System.Web.HttpContext.Current.User asClaimsPrincipal;

    stringrole = GetClaimValue(principal, ClaimTypes.Role);

    stringCompanyId = GetClaimValue(principal, MyClaimTypes.CompanyId);

    stringSuperUser = GetClaimValue(principal, MyClaimTypes.SuperUser);

    //End Temp code Check the claims data in context

    }

    }

    privatestringGetClaimValue(IPrincipal currentPrincipal, stringkey)

    {

    varidentity = currentPrincipal.Identity asClaimsIdentity;

    if(identity == null)

    returnnull;

    varclaim = identity.Claims.FirstOrDefault(c => c.Type == key);

    returnclaim.Value;

    }
    Friday, June 14, 2019 12:07 AM

All replies

  • User-1811426859 posted

    You should add claims before sign-in , before PasswordSignInAsync method .

    Saturday, June 15, 2019 7:59 AM