我将 ASP.NET、MVC 与我自己的数据库一起使用。我试图实现自己的会员提供程序,因为我想使用我的数据库表“用户”。
但现在我很困惑:我已经注册并且我有登录页面。但是在我登录之后,其他控制器和页面如何知道当前登录的用户 ID 是什么?
我在会员提供程序中实现的功能:
public class MyMembershipProvider : MembershipProvider
{
private ManagerdBEntities1 db = new ManagerdBEntities1();
//
// Properties from web.config, default all to False
//
private string _ApplicationName;
private bool _EnablePasswordReset;
private bool _EnablePasswordRetrieval = false;
private bool _RequiresQuestionAndAnswer = false;
private bool _RequiresUniqueEmail = true;
private int _MaxInvalidPasswordAttempts;
private int _PasswordAttemptWindow;
private int _MinRequiredPasswordLength;
private int _MinRequiredNonalphanumericCharacters;
private string _PasswordStrengthRegularExpression;
private MembershipPasswordFormat _PasswordFormat = MembershipPasswordFormat.Hashed;
private string GetConfigValue(string configValue, string defaultValue)
public override void Initialize(string name, NameValueCollection config)
public override string ApplicationName
public override bool ChangePassword(string username, string oldPassword, string newPassword)
public override bool ChangePasswordQuestionAndAnswer(string username,
string password,
string newPasswordQuestion,
string newPasswordAnswer)
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
public override MembershipUser GetUser(string username, bool userIsOnline)
public override string GetUserNameByEmail(string email)
public override string ResetPassword(string username, string answer)
public override bool EnablePasswordReset
public override bool EnablePasswordRetrieval
public override int MaxInvalidPasswordAttempts
public override int MinRequiredNonAlphanumericCharacters
public override int MinRequiredPasswordLength
public override int PasswordAttemptWindow
public override MembershipPasswordFormat PasswordFormat
public override string PasswordStrengthRegularExpression
public override bool RequiresQuestionAndAnswer
public override bool RequiresUniqueEmail
public override bool ValidateUser(string username, string password)
我的用户模型:
public partial class User
{
private ManagerdBEntities1 db = new ManagerdBEntities1();
public MembershipUser CreateUser(string username, string password, string email)
public static void SendEmailThroughGmail(string messageBody, string emailTo)
public User FindUser(string username)
public string GetUserNameWithEmail(string email)
public MembershipUser GetUser(string username)
private static string CreateSalt()
public bool ValidateUser(string username, string password)
private static string CreatePasswordHash(string pwd, string salt)
**
- 这是用户字段:
**
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int PermissionsId { get; set; }
public int MainGroupId { get; set; }
public int MinorGroupId { get; set; }
public string PasswordSalt { get; set; }
public string Comments { get; set; }
[DataType(DataType.Date)]
public System.DateTime CreateDate { get; set; }
public Nullable<System.DateTime> LastModifiedDate { get; set; }
public System.DateTime LastLoginDate { get; set; }
public string LastLoginIP { get; set; }
public bool IsActivated { get; set; }
public bool IslockedOut { get; set; }
public System.DateTime LastLockedoutDate { get; set; }
public string LastLockedOutReason { get; set; }
public string NewPasswordKey { get; set; }
public Nullable<System.DateTime> NewPasswordRequested { get; set; }
public string NewEmail { get; set; }
public string NewEmailKey { get; set; }
public Nullable<System.DateTime> NewEmailRequested { get; set; }
public virtual Permission Permission { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
public virtual UserGroup UserGroup { get; set; }
public virtual UserGroup UserGroup1 { get; set; }