ASP.NET身份认证和权限认证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET身份认证和权限认证相关的知识,希望对你有一定的参考价值。

public partial class admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                {
                    var cookies = Request.Cookies[FormsAuthentication.FormsCookieName];
                    var ticket = FormsAuthentication.Decrypt(cookies.Value);
                    if(!string.IsNullOrEmpty(ticket.Name))
                    {
                        Response.Redirect("index.aspx");
                    }
                }
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            try {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,
                    user_name.Text.Trim(),
                    DateTime.Now,
                    DateTime.Now.AddMinutes(20),
                    true,
                    "admin",
                    "/"
                    );
                
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,FormsAuthentication.Encrypt(ticket));
                cookie.HttpOnly = true;
                Response.Cookies.Add(cookie);

                Response.Redirect("index.aspx");
            }
            catch(Exception ex) {
                Response.Write(ex.Message);
            }
        }
    }

index.aspx.cs

public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                        FormsAuthenticationTicket ticket = id.Ticket;
                        string userData = ticket.UserData;
                        string[] roles = userData.Split(,);
                        //此处根据userData解决isInRole返回False的问题
                        HttpContext.Current.User = new GenericPrincipal(id, roles);
                        
                        Response.Write("管理员" + HttpContext.Current.User.Identity.Name);
                    }

                }
                else
                {
                    Response.Redirect("admin.aspx");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("admin.aspx");
            }



            ////try
            ////{
            ////    if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            ////    {
            ////        var cookies = Request.Cookies[FormsAuthentication.FormsCookieName];
            ////        var ticket = FormsAuthentication.Decrypt(cookies.Value);
            ////        if (!string.IsNullOrEmpty(ticket.Name))
            ////        {
            ////            Response.Write(ticket.Name);
            ////        }
            ////        else
            ////        {
            ////            Response.Redirect("admin.aspx");
            ////        }
            ////    }
            ////    else
            ////    {
            ////        Response.Redirect("admin.aspx");
            ////    }
            ////}
            ////catch (Exception ex)
            ////{
            ////    Response.Redirect("admin.aspx");
            ////}
           
        }
    }

 

以上是关于ASP.NET身份认证和权限认证的主要内容,如果未能解决你的问题,请参考以下文章

Asp.Net实现FORM认证的一些使用技巧(必看篇)

关于ASP.NET MVC的权限认证的一些总结

2021-06-23 .NET高级班 62-ASP.NET Core JWT的权限认证使用(工具版)

ASP.NET Core框架探索之Authentication

Django REST框架--认证和权限

ASP.NET Core认证授权方案