1

I am using asp.net membership. I have login successfully but when I have access user than user.Identity.GetUserId() always null.

this is my login code.

var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
SignInStatus result = signinManager.PasswordSignIn(txtEmail.Text, txtPassword.Text, RememberMe.Checked, false);

switch (result)
{
    case SignInStatus.Success:
        string strReturnUrl = Request.QueryString["ReturnUrl"] ?? "";

        if (string.IsNullOrWhiteSpace(strReturnUrl))
        {
            if (GenericMethods.CheckUserInRole(user, UserRoles.SystemAdmin.ToString()))
                strReturnUrl = "~/Admin/SettingsMaster.aspx";
            else if (GenericMethods.CheckUserInRole(user, UserRoles.Admin.ToString()))
                strReturnUrl = "~/Admin/SettingsMaster.aspx";
            else if (GenericMethods.CheckUserInRole(user, UserRoles.CorporateSponsor.ToString()))
                strReturnUrl = "~/";
            else if (GenericMethods.CheckUserInRole(user, UserRoles.Advertiser.ToString()))
            {

                int productcount = ProductBL.GetInactiveProductCount(user.Id);
                if (String.IsNullOrEmpty(user.FirstName) || String.IsNullOrEmpty(user.LastName) ||
                                String.IsNullOrEmpty(user.Address1) || String.IsNullOrEmpty(user.PhoneNumber) ||
                                String.IsNullOrEmpty(user.Email) || String.IsNullOrWhiteSpace(user.Birthdate.ToString()))
                {
                    strReturnUrl = "~/MyAccount/ProfileSetting.aspx";
                }
                else if (productcount > 0)
                {
                    strReturnUrl = "~/MyAccount/ViewFeatureProduct.aspx";
                }
                else
                    strReturnUrl = "~/MyAccount/AdvertiserTutorial.aspx";

            }
            else if (GenericMethods.CheckUserInRole(user, UserRoles.Subscriber.ToString()))
            {
                int productcount = ProductBL.GetInactiveProductCount(user.Id);
                if (String.IsNullOrEmpty(user.FirstName) || String.IsNullOrEmpty(user.LastName) ||
                    String.IsNullOrEmpty(user.Address1) || String.IsNullOrEmpty(user.PhoneNumber) ||
                    String.IsNullOrEmpty(user.Email) || string.IsNullOrWhiteSpace(user.Birthdate.ToString()))
                {
                    strReturnUrl = "~/MyAccount/ProfileSetting.aspx";
                }
                else if (productcount == 0)
                {
                    strReturnUrl = "~/MyAccount/TradeMyStuff.aspx";
                }
                else
                    strReturnUrl = "~/";
            }
        }
        var userLoginInfo = new UserLoginInfo("ClosetAuctions.com", "CA");
        userManager.AddLogin(user.Id, userLoginInfo);

        user.LastLoginDate = DateTime.Now;
        userManager.Update(user);

        IdentityHelper.RedirectToReturnUrl(strReturnUrl, Response);
        break;
    case SignInStatus.LockedOut:
        Response.Redirect("/Account/Lockout");
        break;
    case SignInStatus.RequiresVerification:
        Response.Redirect(
            String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                Request.QueryString["ReturnUrl"],
                RememberMe.Checked),
            true);
        break;
    // ReSharper disable once RedundantCaseLabel
    case SignInStatus.Failure:
    default:
        FailureText.Text = "Invalid login attempt";
        ErrorMessage.Visible = true;
        break;
}

I am getting userId here.

public static ApplicationUser GetCurrentUser()
{
    var user = HttpContext.Current.User;

    if (user == null) return null;
    if (user.Identity.GetUserId() == null) return null;



    var db = new MyDbContext();
    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    return manager.FindById(user.Identity.GetUserId());

    //   manager.AddClaim(CurrentUser.Id, new Claim(ClaimTypes.Role, "systemAdmin"));
    //   manager.AddClaim(CurrentUser.Id, new Claim(ClaimTypes.Role, "admin"));                
}

I am not sure, where I am wrong.

If any one know then Can you please help me.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Vishal Kiri
  • 1,248
  • 1
  • 12
  • 24

1 Answers1

0

Maybe you check authentication first.

  if (User.Identity.IsAuthenticated)
  {
      string userId = HttpContext.Current.User.Identity.Name;
  } 
Stephen
  • 115
  • 10