C# 如何在Web安全上要求用户名、密码和公司ID。使用简单的成员身份登录MVC4应用程序
我需要为不仅需要用户名和密码,而且还需要公司ID的用户实现登录。用户名仅对一家公司是唯一的,因此可能会多次出现以不同公司ID作为记录的用户名。我试图扩展我当前的简单成员资格提供程序,但我想这对我不起作用()。我的UserProfile表如下所示C# 如何在Web安全上要求用户名、密码和公司ID。使用简单的成员身份登录MVC4应用程序,c#,asp.net-mvc,simplemembership,C#,Asp.net Mvc,Simplemembership,我需要为不仅需要用户名和密码,而且还需要公司ID的用户实现登录。用户名仅对一家公司是唯一的,因此可能会多次出现以不同公司ID作为记录的用户名。我试图扩展我当前的简单成员资格提供程序,但我想这对我不起作用()。我的UserProfile表如下所示 [Table("UserProfile")] public partial class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Ide
[Table("UserProfile")]
public partial class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public int CompanyId { get; set; }
我可以通过用户ID和密码验证用户。我需要做这样的事情:
public class ExtendedSimpleMembershipProvider : SimpleMembershipProvider
{
public override bool ValidateUser(string username, string password, int companyId)
{
int userId = GetUserId(username, companyId);
return SomehowValidateUser(userId, password);
}
private int GetUserId(string username, int companyId)
{
var userId = (from users
in context.UserProfile
where (users.UserName.ToLower() == username) || (users.CompanyId == companyId)
select users.UserId).First();
return userId;
}
}
这是如何工作的?如果您询问在验证用户名和公司ID后如何验证用户名和密码,请尝试直接公开
WebSecurity
类
public override bool ValidateUser(string username, string password, int companyId)
{
// DEV NOTE: change your GetUserId() return to int?
int? userId = GetUserId(username, companyId);
if (userID.HasValue())
return WebSecurity.Login(username, password);
else
return false;
}
如果您询问在验证用户名和公司ID后如何验证用户名和密码,请尝试直接公开
WebSecurity
类
public override bool ValidateUser(string username, string password, int companyId)
{
// DEV NOTE: change your GetUserId() return to int?
int? userId = GetUserId(username, companyId);
if (userID.HasValue())
return WebSecurity.Login(username, password);
else
return false;
}
结果是这样的:
public static bool ValidateUser(string username, string password, string companyName)
{
int companyId = MyRepository.GetCompanyIdByName(companyName);
int? userId = companyId == 0 ? null : MyRepository.GetUserId(username, companyId);
if (userId.HasValue && userId.Value != 0)
{
var userKey = username + "@" + companyName.ToLower();
return WebSecurity.Login(userKey, password);
}
else
{
return false;
}
}
结果是这样的:
public static bool ValidateUser(string username, string password, string companyName)
{
int companyId = MyRepository.GetCompanyIdByName(companyName);
int? userId = companyId == 0 ? null : MyRepository.GetUserId(username, companyId);
if (userId.HasValue && userId.Value != 0)
{
var userKey = username + "@" + companyName.ToLower();
return WebSecurity.Login(userKey, password);
}
else
{
return false;
}
}
你能给钥匙加上密码吗?旁注-是什么阻止同一用户在两家公司创建相同的帐户?用户仅由公司管理员创建,因此不同公司中可能有同名用户。我使用的是简单成员身份,因此我的密码不在UserProfile的表中。您可以将密码添加到密钥中吗?旁注-是什么阻止同一用户在两家公司创建相同的帐户?用户仅由公司管理员创建,因此不同公司中可能有同名用户。我使用的是简单的成员身份,所以我的密码不在UserProfile的表中。事实证明,我无法重写ValidateUser(string,string,int)不存在的方法。因此,我应该以某种方式扩展成员资格,使其也具有ValidateUser方法。事实证明,我无法重写不存在ValidateUser的方法(string,string,int)。因此,我应该以某种方式扩展成员资格,使其也具有ValidateUser方法。