【问题标题】:How to set asp.net authenticated properties如何设置 asp.net 认证的属性
【发布时间】:2009-06-15 14:57:54
【问题描述】:

我的 web.config 文件中有以下设置。如果用户未登录,它基本上会限制对页面的访问。如果我不想使用 asp 登录控件或实现成员资格提供程序,我如何“告诉”asp loginregister.aspx 页面已授权请求如果我想实现自己的登录系统?

谢谢。

<authentication mode="Forms">
            <forms loginUrl="~/loginregister.aspx"
                   name=".ASPXFORMSAUTH" />
        </authentication>

        <authorization>
            <deny users="?" />
        </authorization>

<location path="~/secretpage.aspx" allowOverride="true">
        <system.web>
            <compilation debug="true" />
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
    </location>

【问题讨论】:

    标签: c# asp.net-membership


    【解决方案1】:

    验证用户后,设置票证....

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

    使用这个帮助类...

    如果使用登录控件,请在 Authenticated 事件处理程序中执行。

    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;
            }
        }
    

    【讨论】:

    • 只是一个有用的说明。要再次从票证中取出 userData,您需要将 User.Identity 转换为 FormsIdentity 才能看到票证。
    【解决方案2】:
        // 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);
    

    表单身份验证确实暴露了 .Net 4.0 FormsAuthentication.Timeout.TotalMinutes 的超时

    【讨论】:

      【解决方案3】:

      如果您不想对 .NET 系统做任何事情,那会有点困难。

      如果您同意,只需在登录时使用“FormsAuthentication.RedirectFromLoginPage”来设置显示用户已登录的 cookie。

      【讨论】:

        猜你喜欢
        • 2013-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多