c# - 如何设置 ASP.NET 身份验证属性

标签 c# asp.net-membership

我的 web.config 文件中有以下设置。如果用户未登录,它基本上会限制对页面的访问。如果我不想使用 asp 登录控件或实现成员(member)资格提供程序,我如何“告诉”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>

最佳答案

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

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

使用这个辅助类...

如果使用登录控件,请在经过身份验证的事件处理程序中执行此操作。

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

关于c# - 如何设置 ASP.NET 身份验证属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/996588/

相关文章:

c# - 如何从 ascx.cs 文件中的 ascx 文件中的 FormView 中的文本框中获取文本值

c# - 在 C# 中使用 excel 中的模板

asp.net - 当用户未登录时,ASP MVC 成员资格无法显示图像

ASP.NET 成员身份在没有 requiresQuestionAndAnswer 的情况下创建用户

asp.net-mvc-4 - 如何从asp.net mvc 4中的空Web应用程序模板制作简单的成员(member)资格提供程序?

asp.net - 更改用户名 ASP.net MVC 3 成员资格

c# - 异步网络操作永远不会完成

c# - 无法将类型 'object' 隐式转换为 'Microsoft.Office.Interop.Excel.Range'

C# ASP.NET MVC Controller 中的关联数组

c# - ASP.NET MVC 自定义成员资格提供程序 - 如何重载 CreateUser?