مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

morteza373

عضویت از 1394/10/30

کار نکردن FormAuthentication TimeOut

  • دوشنبه 4 آذر 1398
  • 11:23
تشکر میکنم

سلام خسته نباشید

من تو پروژه از FormAuthentication استفاده کردم و هنگام لاگین از دستور FormAuthentication.SetAuthCookie استفاده کردم.

حالا میخوام تایم اوت تنظیم کنم، تو وب کانفیگ کد زیر رو گذاشتم ولی کار نمیکنه:

    <authentication mode="Forms">
      <forms loginUrl="/Login/Index" cookieless="UseCookies" slidingExpiration="true" timeout="10" />
    </authentication>

با FormAuthenticationTicket هم تست کردم جواب نداد ،خود کوکی رو هم براش expire تنظیم کردم اونم کار نکرد ،لطفا راهنمایی کنید چیکار کنم؟

ممنون.

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

morteza373

عضویت از 1394/10/30

  • دوشنبه 4 آذر 1398
  • 12:33

مشکلم حل شد ، گفتم بزار راه حل رو بزارم شاید کسی به دردش بخوره:

من تو فایل Global.asax اومده بودم در رویداد FormsAuthentication_OnAuthenticate با کد زیر لاگین بودن کاربر رو چک کرده بودم و بعد دسترسی رو اعمال میکردم :

        protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
        {
            if (FormsAuthentication.CookiesSupported == true)
            {
                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                {
//اعمال دسترسی
                }
                else
                {
                    e.User = null;
                }
            }
        }

که روش بالا خوب نیست برای لاگین و دسترسی جواب میده ولی تایم اوت رو از وب کانفیگ نمیخونه ، حالا روش جدید :

   

protected void Application_AuthenticateRequest(object sender, EventArgs e)

    {

        // look if any security information exists for this request

        if (HttpContext.Current.User != null)

        {

            // see if this user is authenticated, any authenticated cookie (ticket) exists for this user

            if (HttpContext.Current.User.Identity.IsAuthenticated)

            {

                // see if the authentication is done using FormsAuthentication

                if (HttpContext.Current.User.Identity is FormsIdentity)

                {

                    // Get the roles stored for this request from the ticket

                    // get the identity of the user

                    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;

                    //Get the form authentication ticket of the user

                    FormsAuthenticationTicket ticket = identity.Ticket;

                    //Get the roles stored as UserData into ticket

                    string[] roles = ticket.UserData.Split(',');

                    //Create general prrincipal and assign it to current request

                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);

                }

            }

        }

    }

با روش بالا خیلی خوب کار میکنه ،روش بالا رو از لینک زیر پیدا کردم شاید به درد دوستان بخوره:

http://venkateswarlu.net/DotNet/How_to_Implement_Forms_Authentication_Tickets.aspx

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)