【问题标题】:Validate user details验证用户详细信息
【发布时间】:2014-02-06 14:47:11
【问题描述】:

我试图让 UserPro 类从 User 类中读取方法,但我一直遇到“当前上下文中不存在名称‘密码’”和“当前上下文中不存在名称‘用户名’ " 在以下代码中:

public class UserPro : IProvidePrincipal
{

    private User _userRepo;

    public UserPro (User userRepo)
   {
    this._userRepo = userRepo;
   }

    public IPrincipal CreatePrincipal(string username, string password)
    {
        try 
        {
            if (!_userRepo.Validate(UserName, Password))
            {
                return null;
            }

            else
        {
            var identity = new GenericIdentity(username);
            IPrincipal principal = new GenericPrincipal(identity, new[] { "Admin" });

            return principal;
        }
    }
    catch
    {
        return null;
    }
    }   
}

用户类别:

 public bool Validate(string UserName, string Password)
    {
        var user = db.api_login.FirstOrDefault(u => u.username == UserName
                 && u.password == Password);

        if (user != null)
        {
            if (user.password == Password)
            {
                return true;
            }
        }

        return false;
    }

我在方法中哪里出错了有什么建议吗?非常感谢

【问题讨论】:

    标签: c# asp.net asp.net-mvc-4 asp.net-web-api basic-authentication


    【解决方案1】:

    我认为用户名的 U 和密码的 P 应该很小。 示例:

    if (!_userRepo.Validate(userName, password))
                {
                    return null;
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-30
      • 2011-05-23
      • 1970-01-01
      • 2018-11-18
      • 2011-09-24
      • 2014-02-05
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多