با سلام.
تو session start نوشتم که تعداد افراد انلاین شمارش بشه.
اما نمی دونم چرا احرا نمیشه
لطفا راهنمایی نمایید
با تشکر
شاید اشتباه تایپی دارید
کد کامل global را قرار دهید
protected void Application_Start() { HttpContext.Current.Application["OnlineUser"] = 0; AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } protected void Session_Start() { HttpContext.Current.Application["OnlineUser"] = ((int)HttpContext.Current.Application["OnlineUser"] + 1); StateClass.CounterState(); } protected void Session_End() { if (Application["OnlineUser"] != null) { HttpContext.Current.Application["OnlineUser"] = ((int)HttpContext.Current.Application["OnlineUser"] - 1); StateClass.CounterState(); } }
استاد فکر می کنم تو identity از session_start پیشتیبلتی نمی شود
من تو webform برای شمارش همین نوع متغیر رو تعریف می کردم و درست جواب می داد
اما نمی دونم چرا اینحا اذیت می کنه
لطفا راهنمایی نمایید
با تشکر
با سلام
دوست عزیز از روش زیر استفاده کنید
public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (HttpRuntime.Cache["LoggedInUsers"] != null) //if the list exists, add this user to it { //get the list of logged in users from the cache List<string> loggedInUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"]; //add this user to the list loggedInUsers.Add(model.UserName); //add the list back into the cache HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers; } else //the list does not exist so create it { //create a new list List<string> loggedInUsers = new List<string>(); //add this user to the list loggedInUsers.Add(model.UserName); //add the list into the cache HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers; } if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(model); } public ActionResult LogOff() { string username = User.Identity.Name; //get the users username who is logged in if (HttpRuntime.Cache["LoggedInUsers"] != null)//check if the list has been created { //the list is not null so we retrieve it from the cache List<string> loggedInUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"]; if (loggedInUsers.Contains(username))//if the user is in the list { //then remove them loggedInUsers.Remove(username); } // else do nothing } //else do nothing FormsAuthentication.SignOut(); return RedirectToAction("Index", "Home"); }
و با کد زیر میتونید نام کاربران و تعداد کل کاربران رو به دست بیارید
@if (HttpRuntime.Cache["LoggedInUsers"] != null) { List<string> LoggedOnUsers = (List<string>)HttpRuntime.Cache["LoggedInUsers"]; if (LoggedOnUsers.Count > 0) { <div class="ChatBox"> <ul> @foreach (string user in LoggedOnUsers) { <li> <div class="r_row"> <div class="r_name">@Html.Encode(user)</div> </div> </li> } </ul> </div> } }
با تشکر از دوست عزیز
من از identity استفاده می کنم اما شما از membership استفاده می کنید.
راه دیگری نمی شناسید؟
ربطی به Identity نداره دوست من
رویداد ها باید اجرا بشه
استاد چه چیزی رو سرچ کنم؟
شما از همین کد استفاده می کنید؟
من از همین کد استفاده میکنم
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)