简体   繁体   中英

How to pass user_name based on the user_ID from login page to home page using mvc

When i login my login page it goes to home page with my user name it working fine and i want userID based on my user_name from login to home page

<section id="login">
                                @if (Request.IsAuthenticated)
                                {
                                    <text>
                                        <button class="dropdown-toggle" id="dd-user-menu" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                             <img src="~/img/avatar-2-64.png" alt="">
                                        </button>
                                        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dd-user-menu">
                                            <a class="dropdown-item"><span class="font-icon glyphicon glyphicon-user"></span>Hello, @User.Identity.Name</a>
                                            <a class="dropdown-item" href="#"><span class="font-icon glyphicon glyphicon-cog"></span>Settings</a>
                                            <a class="dropdown-item" href="#"><span class="font-icon glyphicon glyphicon-question-sign"></span>Help</a>
                                            <div class="dropdown-divider"></div>
                                            @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
                                            {
                                                @Html.AntiForgeryToken()
                                                <a class="dropdown-item" href="javascript:document.getElementById('logoutForm').submit()"><span class="font-icon glyphicon glyphicon-log-out"></span>Logout</a>
                                            }
                                        </div>
                                    </text>
                                }
                            </section>

but in my above code i get user_name so i want to get user_id as well

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Login login)
        {
            if (ModelState.IsValid)
            {
                bool success = WebSecurity.Login(login.UserName, login.Password, false);
                var UserID = GetUserID_By_UserName(login.UserName);
                var DocID = GetDocID_By_UserName(login.UserName);

                if (success == true)
                {
                    Session["Name"] = login.UserName;
                    Session["UserID"] = UserID;
                    Session["RefID"] = DocID;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("Error", "Please enter valid Username and Password");
                    return View(login);
                }

            }
            return View(login);
        }
        #endregion

in my above code i get user_name so i want to get user_id as well

You cannot call User.Identity.XXX . You cannot add custom property to Principle class.

However, you could just call Session["UserID"] inside View, since you already store UserID in session state.

For example,

<div>@Convert.ToInt32(Session["UserID"] ?? 0)</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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