我的会员提供者 - 如何找到当前用户

时间:2013-01-20 09:20:04

标签: asp.net-mvc

我使用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; }

1 个答案:

答案 0 :(得分:0)

您可以使用Membership.GetUser()获取当前登录的用户

MSDN Documentation