获取user.Identity.GetUserId()始终为null

时间:2015-10-22 10:12:34

标签: asp.net asp.net-membership

我正在使用asp.net会员资格。我已成功登录,但当我访问用户而不是user.Identity.GetUserId()时始终为null。

这是我的登录代码。

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

我在这里得到userId

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"));                
}

我不确定,我错了。

如果有人知道,那么请你帮助我。

1 个答案:

答案 0 :(得分:0)

也许您先检查身份验证。

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