0

シンプルなメンバーシップ OOTB アカウント コントローラーを使用する mvc4 フォーム アプリケーションがあります。次のように、登録が完了した後にユーザー名を正常に取得できたアプリケーションのビューモデルがあります。

this.UserName = HttpContext.Current.User.Identity.Name;

これが機能していた時点で、私の登録方法は次のとおりでした:

try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new
                {
                    //Form defined values
                    Forename = model.Forename,
                    Surname = model.Surname,
                    Email = model.Email,
                    Password = model.Password,
                    Answer = model.SecretAnswer,
                    DOB = model.DOB,

                    //Auto defined values
                    JoinDate = DateTime.Today,
                    LastLogin = DateTime.Now,
                    CompanyID = 5,
                    ParticipationPoints = 0,
                    Privacy = 0,
                    IsDeleted = 0,
                    ImageURL = "/Images/user-holder.jpg"

                });
                WebSecurity.Login(model.UserName, model.Password);
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }

私の顧客と相談した結果、誰かがインターネットに登録するのを防ぐために、既存のユーザーとして見つかったユーザー名の値を持つユーザーテーブルに既に含まれている必要があることが決定されました。したがって、この後、登録は次のように変更されました。

コントローラ

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            avm.Username = model.UserName;
            avm.Forename = model.Forename;
            avm.Surname = model.Surname;
            avm.Email = model.Email;
            avm.Password = model.Password;
            avm.Answer = model.SecretAnswer;
            avm.DOB = model.DOB;

            avm.RegisterUser();
            if (avm.StatusCode == "Success")
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                //ModelState.AddModelError("", ErrorCodeToString(avm.StatusCode));
                return View();
            }
       }
   }

ビューモデル

try
        {
            this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);

            userRepository = new Repository<MyUser>(dbcontext);

            //Step 1 - Check User is in user table.
            MyUser userCheck = userRepository.Get(u => u.Username == this.Username).ToList().FirstOrDefault();

            if (userCheck == null)
            {
                StatusCode = "NoUserError";
                return;
            }
            else
            {
                //Step 2 - Check user has not already registered
                if (userCheck.Password != null || userCheck.Answer != null)
                {
                    StatusCode = "AlreadyRegistered";
                    return;
                }
            }

            //Step 3 - Check the email is valid and the password confirms to password length.
            Regex expEmail = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            if (!expEmail.IsMatch(this.Email))
            {
                StatusCode = "InvalidEmail";
                return;
            }

            if (this.Password.Length < 8)
            {
                StatusCode = "InvalidPassword";
                return;
            }

            //Encrypt the password to store in SQL Azure. It does not at this point have any encryption.
            EncryptionUtils encryptor = new EncryptionUtils();
            string encrytpedPassword = encryptor.Encrypt(this.Password);

            //Form defined fields
            userCheck.Username = this.Username;
            userCheck.Password = encrytpedPassword;
            userCheck.Forename = this.Forename;
            userCheck.Surname = this.Surname;
            userCheck.Email = this.Email;
            userCheck.Answer = this.Answer;
            userCheck.DOB = this.DOB;

            //Automatically defined values
            userCheck.JoinDate = DateTime.Today;
            userCheck.LastLogin = DateTime.Now;
            userCheck.CompanyID = 5;
            userCheck.RoleID = 3;
            userCheck.ParticipationPoints = 0;
            userCheck.Privacy = 0;
            userCheck.IsDeleted = false;
            userCheck.ImageURL = "/Images/user-holder.jpg";

            userRepository.Update(userCheck);
            userRepository.SaveChanges();

            StatusCode = "Success";
        }
        catch (Exception ex)
        {
            StatusCode = "Error";
            return;
        }

 }

ホームコントローラーにアクセスすると、HttpContext.Current.User.Identity.Name 値にアクセスできません。変更により、認証されたユーザー名が別の場所に保存されましたか?

4

1 に答える 1

1

登録成功後に認証クッキーを発行する必要があります。試す、

if (avm.StatusCode == "Success")
{
    FormsAuthentication.SetAuthCookie(model.UserName, false);
    return RedirectToAction("Index", "Home");
}

お役に立てれば。

于 2013-10-14T21:53:45.390 に答える