繁体   English   中英

在asp.net mvc中注册成功后用户名自动填写登录页面(重定向到登录)

[英]Username auto fill in Login Page after Register success (Redirect to Login) in asp.net mvc

注册成功后,我重定向到我的登录页面。 事实上,当我重定向到登录页面并在登录页面用户名输入中显示时,如何携带我的注册用户名。

我的控制器

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Account account)
        {
            if (!db.Accounts.Any(x => x.Username.Equals(account.Username)))
            {
                Account acc = new Account()
                {
                    Username = account.Username,
                    Password = account.Password,
                    CreatedDate = DateTime.UtcNow,
                    IsDeleted = false,
                };
                db.Accounts.Add(acc);
                db.SaveChanges();

                FormsAuthentication.SetAuthCookie(acc.AccountID.ToString(), false);
                ViewBag.Success = "User registered. You may login now!";
                return RedirectToAction("Login", "Account");
            }
            else
            {
                ViewBag.Error = "User already exist!";
                return View();
            }
        }

登录用户名输入

<input type="text" name="Username" class="form-control" placeholder="Username">

有两种方法可以在登录操作结果中获取用户名。

  1. 在 RedirectToAction 中传递匿名对象。
    return RedirectToAction("Login", "Account", new {username=account.username});
  1. 从 FormsAuthentication 票中获取用户名
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie != null)
    {
        // Get the forms authentication ticket.
        FormsAuthenticationTicket authTicket = 
        FormsAuthentication.Decrypt(authCookie.Value);
        var identity = new GenericIdentity(authTicket.Name, "Forms");
        var principal = new MyPrincipal(identity);
        // Get the custom user data encrypted in the ticket.
        string userData = ((FormsIdentity)(Context.User.Identity)).Ticket.UserData;
        // Deserialize the json data and set it on the custom principal.
        var serializer = new JavaScriptSerializer();
        principal.User = (User)serializer.Deserialize(userData, typeof(User));
        // Set the context user.
        Context.User = principal;
    }

您可以为登录创建重载。

public ActionResult Login(string userName)
{
   return View();
}

 [HttpPost]
 public ActionResult Register(Account account)
 {
   ...
   return RedirectToAction("Login", "Account", new {userName=account.Username});               

 }

编辑:要隐藏用户名值,您可以应用基本的 base64 编码。

public string Encode(string strToEncode)
{
    byte[] encodedVal = System.Text.Encoding.UTF8.GetBytes(strToEncode);
    return Convert.ToBase64String(encodedVal);
}

public string Decode(string strToDecode)
{
    byte[] decodedVal = Convert.FromBase64String(strToDecode);
    return System.Text.Encoding.UTF8.GetString(decodedVal);
}

public ActionResult Login(string userName)
{
   var userNameDecoded = Decode(userName);
   return View();
}

[HttpPost]
public ActionResult Register(Account account)
{
   ...
   return RedirectToAction("Login", "Account", new {userName=Encode(account.Username)});               

}

这里有两种常用的方式将数据从一个控制器发送到另一个控制器

  1. 可以传数据return RedirectToAction("Login", "Account", new {userName=account.Username}); 并得到它

    公共 ActionResult 登录(字符串用户名){ 返回视图(); }

  2. 或者您可以简单地使用 tempdata

Set ==> TempData["UserName"] = account.Username;

Get ==> var userName = TempData["UserName"].ToString();

首先,当您重定向到登录操作时,将用户名传递给它并在登录操作中接收用户名,然后将用户名存储在 viewBag 中。 在视图上使用 viewBag 设置文本值

public ActionResult Login(string UserName)
        {
            ViewBag.UserName=username; 
            return View();
        }

        [HttpPost]
        public ActionResult Register(Account account)
        {
            if (!db.Accounts.Any(x => x.Username.Equals(account.Username)))
            {
                Account acc = new Account()
                {
                    Username = account.Username,
                    Password = account.Password,
                    CreatedDate = DateTime.UtcNow,
                    IsDeleted = false,
                };
                db.Accounts.Add(acc);
                db.SaveChanges();

                FormsAuthentication.SetAuthCookie(acc.AccountID.ToString(), false);
                ViewBag.Success = "User registered. You may login now!";
                return RedirectToAction("Login", "Account",account.Username);
            }
            else
            {
                ViewBag.Error = "User already exist!";
                return View();
            }
        }

在查看

<input type="text" name="Username" class="form-control" value="@ViewBag.UserName" placeholder="Username">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM