How to set asp.net authenticated properties

前端 未结 3 977
野的像风
野的像风 2021-01-01 08:02

I have the following settings in my web.config file. It basically restricts access to a page if the user is not logged in. If I don\'t want to use the asp login controls or

相关标签:
3条回答
  • 2021-01-01 08:50

    If you don't want to do ANYTHING with the .NET system, it is going to be a bit hard.

    If you are ok with some, just use the "FormsAuthentication.RedirectFromLoginPage" on your login to set the cookie that shows a user as logged in.

    0 讨论(0)
  • 2021-01-01 08:58

    After you validate your user, set a ticket....

        Response.Cookies.Add(TicketHelper.CreateAuthCookie(Login1.UserName, userData, Login1.RememberMeSet /*persistent cookie*/));
    

    using this helper class...

    If using a login control, do it in Authenticated event handler.

    using System;
    using System.Web;
    using System.Web.Security;
    
    namespace CustomAuthRepurposingFormsAuth
    {
        public static class TicketHelper
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="userName"></param>
            /// <param name="userData">be mindful of the cookie size or you will be chasing ghosts</param>
            /// <param name="persistent"></param>
            /// <returns></returns>
            public static HttpCookie CreateAuthCookie(string userName, string userData, bool persistent)
            {
                DateTime issued = DateTime.Now;
                // formsAuth does not expose timeout!? have to hack around the
                // spoiled parts and keep moving..
                HttpCookie fooCookie = FormsAuthentication.GetAuthCookie("foo", true);
                int formsTimeout = Convert.ToInt32((fooCookie.Expires - DateTime.Now).TotalMinutes);
    
                DateTime expiration = DateTime.Now.AddMinutes(formsTimeout);
                string cookiePath = FormsAuthentication.FormsCookiePath;
    
                var ticket = new FormsAuthenticationTicket(0, userName, issued, expiration, true, userData, cookiePath);
                return CreateAuthCookie(ticket, expiration, persistent);
            }
    
            public static HttpCookie CreateAuthCookie(FormsAuthenticationTicket ticket, DateTime expiration, bool persistent)
            {
                string creamyFilling = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, creamyFilling)
                                 {
                                     Domain = FormsAuthentication.CookieDomain,
                                     Path = FormsAuthentication.FormsCookiePath
                                 };
                if (persistent)
                {
                    cookie.Expires = expiration;
                }
    
                return cookie;
            }
        }
    
    0 讨论(0)
  • 2021-01-01 09:00
        // formsAuth does not expose timeout!? have to hack around the
        // spoiled parts and keep moving..
        HttpCookie fooCookie = FormsAuthentication.GetAuthCookie("foo", true);
        int formsTimeout = Convert.ToInt32((fooCookie.Expires - DateTime.Now).TotalMinutes);
    

    Forms Authentication does expose timeout as of .Net 4.0 FormsAuthentication.Timeout.TotalMinutes

    0 讨论(0)
提交回复
热议问题