私は自分のDBでASP.NET、MVCを使用しています。DBテーブル「users」を使用したかったので、独自のメンバーシッププロバイダーを実装しようとしました。
しかし今、私は混乱しています。私には登録があり、ログインページが機能しています。しかし、私がログインした後、他のコントローラーとページは、現在ログインしているユーザー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; }