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

  • Question

  • 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;

    }


    Thursday, June 13, 2019 8:43 AM

All replies

  • Hi Raj,

    Welcome to the MSDN forum.

    According to the description, this issue is about the ASP.NET development and our forum is to discuss the .NET Framework installation issues, It seems you have already reported this: https://forums.asp.net/t/2156624.aspx on the appropriate forum and please follow it, you will get a better support from there, thanks for your understanding.

    If you have any other issues in the future, please feel free to let us know.

    Have a nice day!

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, June 14, 2019 2:58 AM