代码之家  ›  专栏  ›  技术社区  ›  Ross Kelly

如何防止用户访问其他用户的配置文件?

  •  0
  • Ross Kelly  · 技术社区  · 7 年前

    我有一个名为 用户登录 用于验证用户身份的。“Candidate”参数是一个模型,其中包含联系人的电子邮件地址和密码等字段。

    该模型还包含“AgencyID”和“ContactID”字段。使用它们是为了让我知道要连接到哪个数据库(AgencyID)和要获取哪个联系人记录(ContactID)。登录的用户是代理的联系人。

    [HttpPost()]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> UserSignIn(Candidate can)
    {
        bool is_err = false;
        string err = string.Empty;
        Candidate c_signed_in = new Candidate();
    
        // check data
        if (string.IsNullOrEmpty(can.Email))
        {
            is_err = true;
            err += "<li>Missing email address.</li>";
        }
        if (string.IsNullOrEmpty(can.AccountPassword))
        {
            is_err = true;
            err += "<li>Missing password.</li>";
        }
    
        // get candidate
        if (ModelState.IsValid && !is_err)
        {
            c_signed_in = await Repository.GetCandidate(can.AgencyID, 0, can.Email.ToLower(), can.AccountPassword, hostingEnv.WebRootPath);
            if (c_signed_in.ContactID == 0)
            {
                is_err = true;
                err += "<li>No account found. Check your credentials.</li>";
            }
        }
    
        // check model state
        if (!ModelState.IsValid || is_err)
        {
            Candidate c_current = await Repository.GetBlankCandidate(can, false);
            c_current.IsModeSignIn = true;
            if (is_err)
                c_current.ErrsSignIn = "<ul class=\"text-danger\">" + err + "</ul>";
            return View("Agency", c_current);
        }
    
        // create claims
        var claims = new List<Claim>
        {
            //new Claim(ClaimTypes.Name, c_signed_in.FirstName + gFunc.SPACE + c_signed_in.FamilyName),
            new Claim(ClaimTypes.Sid, c_signed_in.ContactID.ToString()),
            new Claim(ClaimTypes.Email, c_signed_in.Email)
        };
    
        // create identity
        var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); // cookie or local
    
        // create principal
        ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
    
        // sign-in
        await HttpContext.SignInAsync(scheme: CookieAuthenticationDefaults.AuthenticationScheme, principal: principal);
    
        // add to log
        gFunc.AddLogEntry("SignIn Candidate: " + c_signed_in.FirstName + gFunc.SPACE + c_signed_in.FamilyName + " - " + c_signed_in.Email);
    
        // fini
        return RedirectToAction("Profile", new { agencyID = c_signed_in.AgencyID, contactID = c_signed_in.ContactID });
    }
    

    成功后,此方法重定向到一个名为“Profile”的方法,该方法显示用户的配置文件。

    [HttpGet]
    [Authorize]
    public async Task<ActionResult> Profile(int agencyID, int contactID)
    {
        Candidate can = await Repository.GetCandidate(agencyID, contactID, string.Empty, string.Empty, hostingEnv.WebRootPath);
        if (can.ContactID == 0)
        {
            int id = agencyID;
            return RedirectToAction("Agency", new { agencyID = id });
        }
        return View("Profile", can);
    }
    

    如何避免这种情况?显然,我不能将密码作为参数包含在 简况 方法,因为它只在URL中可见。我应该采取什么方法?

    更新-已解决

    谢谢大家的评论。卡米洛·特里文托的回答解决了我的问题。

    用户登录 方法并删除 简况

    我唯一要改变的就是直接的演员阵容。我的编译器不喜欢它,所以我将它改为使用解析:

        int agency_id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
        int contact_id = int.Parse(User.FindFirst(ClaimTypes.Sid).Value);
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Camilo Terevinto Chase R Lewis    7 年前

    您可以添加 联系人ID 权利要求书:

    new Claim(ClaimTypes.Sid, c_signed_in.ContactID.ToString()),
    new Claim(ClaimTypes.Email, c_signed_in.Email),
    new Claim(ClaimTypes.NameIdentifier,c_signed_in.agencyID.ToString())
    

    [HttpGet]
    [Authorize]
    public async Task<ActionResult> Profile()
    {
        int agencyID = (int)User.FindFirst(ClaimTypes.NameIdentifier).Value
        int contactID = (int) User.FindFirst(ClaimTypes.Sid).Value
    
        Candidate can = await Repository.GetCandidate(agencyID, contactID, string.Empty, string.Empty, hostingEnv.WebRootPath);
        if (can.ContactID == 0)
        {
            int id = agencyID;
            return RedirectToAction("Agency", new { agencyID = id });
        }
    
        return View("Profile", can);
    }