سؤال

هنا هو رمز بلدي لتسجيل الدخول

 var expire = DateTime.Now.AddDays(7);
        // Create a new ticket used for authentication
        var ticket = new FormsAuthenticationTicket(
        1, // Ticket version
        username, // Username to be associated with this ticket
        DateTime.Now, // Date/time issued
        expire, // Date/time to expire
        true, // "true" for a persistent user cookie (could be a checkbox on form)
        roles, // User-data (the roles from this user record in our database)
        FormsAuthentication.FormsCookiePath); // Path cookie is valid for

        // Hash the cookie for transport over the wire
        var hash = FormsAuthentication.Encrypt(ticket);
        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash) { Expires = expire };

        // Add the cookie to the list for outbound response
        Response.Cookies.Add(cookie);

هنا هو رمزي للتحقق من الأدوار. إنها وحدة IHTTP مخصصة

 if (HttpContext.Current.User == null) return;
        if (!HttpContext.Current.User.Identity.IsAuthenticated) return;
        if (!(HttpContext.Current.User.Identity is FormsIdentity)) return;

        // Get Forms Identity From Current User
        var id = (FormsIdentity)HttpContext.Current.User.Identity;
        // Get Forms Ticket From Identity object
        var ticket = id.Ticket;
        // Retrieve stored user-data (our roles from db)
        var userData = ticket.UserData;
        var roles = userData.Split(',');
        // Create a new Generic Principal Instance and assign to Current User
        Thread.CurrentPrincipal = HttpContext.Current.User = new GenericPrincipal(id, roles);

هنا هو رمزي لتسجيل الخروج

FormsAuthentication.SignOut();
        Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
        Session.Clear(); 
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
        Response.AppendHeader("Pragma", "no-cache");
        return View("SignIn");

هذا جنون. لدي اثنين من المواقع الأصلع الآن.

هل كانت مفيدة؟

المحلول

1) لا ينبغي أن تدعوك إلى الاستجابة. Cookies.Remove (Showsauthentication.formscookiename)؛ كن استجابة .Cookies.Remove (أي اسم المستخدم - هو)؛

2) حاول إرسال ملف تعريف الارتباط منتهية الصلاحية إلى المتصفح.

FormsAuthentication.SignOut();

// replace with username if this is the wrong cookie name
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Session.Clear(); 
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");

// send an expired cookie back to the browser
var ticketExpiration    = DateTime.Now.AddDays(-7);
var ticket              = new FormsAuthenticationTicket(
    1, 
    // replace with username if this is the wrong cookie name
    FormsAuthentication.FormsCookieName, 
    DateTime.Now, 
    ticketExpiration, 
    false, 
    String.Empty);
var cookie              = new System.Web.HttpCookie("user")
{
    Expires             = ticketExpiration,
    Value               = FormsAuthentication.Encrypt(ticket),
    HttpOnly            = true
};

Response.Cookies.Add(cookie);

return View("SignIn");

نصائح أخرى

لا يمكنك حذف ملف تعريف الارتباط مباشرة على جهاز كمبيوتر عميل. عند استدعاء ملفات تعريف الارتباط. طريقة Remove يتم حذف ملف تعريف الارتباط على جانب الخادم. لحذف ملف تعريف الارتباط على جانب العميل، من الضروري تعيين تاريخ انتهاء ملف تعريف الارتباط إلى تاريخ سابق.

HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
     cookie.Expires = DateTime.Now.AddDays(-1);
     HttpContext.Current.Response.Cookies.Add(cookie);
}

آمل أن يكون هذا يساعدك.

إذا كنت ترغب في تطبيق سلوك "عدم استعادة التخزين المؤقت على المستعرض" على جميع الصفحات، فيجب عليك وضعه في Global.asax.

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}

آمل أن يساعد شخص ما!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top