zoukankan      html  css  js  c++  java
  • Asp.net身份验证和授权

    Asp.net身份验证和授权

    身份验证是确定用户身份的过程.在用户通过了身份验证后,开发人员就可以确定该用户是否有权继续操作.如果没有进行身份验证,就不能进行实体的授权.
    授权是确定已验证用户是否有权访问应用程序中的某个部分,某个点,或只能访问应用程序提供的特定数据集.对用户和组进行身份验证和授权后,就可以根据用户类型或配置定制站点.
    asp.net身份验证方式:
    1.基本身份验证
    2.摘要身份验证
    3.窗体身份验证
    4.Passport身份验证
    5.集成的Windows验证
    还可以开发自己的验证方法.

    <system.web>
    <authentication mode="Forms">
    <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    ...
    </system.web>

    可用的选项
    <authentication mode="Windows|Forms|Passport|None">

    Forms:未通过验证的请求使用HTTP客户端重定向到一个HTML窗体上.用户要提供登录信息,并提交窗体.
    如果应用程序验证该请求,系统就发送一个窗体,该窗体包含重新获得身份的证书或密钥.

      protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {
                HttpApplication app = (HttpApplication)sender;
                HttpContext context = app.Context; //获取本次Http请求的HttpContext对象 
                 if (context.Request.IsAuthenticated) //验证过的一般用户才能进行角色验证 
                 {
                  FormsIdentity Id = (FormsIdentity)context.User.Identity; //当前用户标识
                    FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份证票 
                    string[] Roles = Ticket.UserData.Split(','); //将角色数据转成字符串数组,得到相关的角色信息 
                    context.User = new System.Security.Principal.GenericPrincipal(Id, Roles); //重新生成带有角色信息的用户
                 }
            }
    
    
    
     string userId = "xx";
              string pa = "123";
    
               string roles = "Administrator";  //从其他地方取得用户角色数据
    
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userId, DateTime.Now, DateTime.Now.AddMinutes(30), true, roles); //建立身份验证票对象 
                string HashTicket = FormsAuthentication.Encrypt(Ticket); //加密序列化验证票为字符串 
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket); //生成Cookie 
              Context.Response.Cookies.Add(UserCookie); //票据写入Cookie 
              Response.Redirect("Info.aspx");
    

      

     if (this.UserName.Text == "xx" && this.Password.Text == "00")
                {
                    FormsAuthentication.RedirectFromLoginPage(this.UserName.Text, true);
                }
                else this.FailureText.Text = "用户名或密码错误";
    

      

  • 相关阅读:
    HDFS入门详解
    Linux find example
    你想建设一个能承受500万PV/每天的网站吗?服务器每秒要处理多少个请求才能应对?
    find 与 tar命令连用
    莫名其妙的主机名 VM_32_234_centos
    hadoop Safe mode is ON 的解决办法
    Does not contain a valid host:port authority: Master:8031 (configuration property 'yarn.resourcemanager.resource-tracker.address')
    Equals 和 == 的区别--转
    线程间操作无效: 从不是创建控件的线程访问它。
    C# EventHandler委托事件小结--百度
  • 原文地址:https://www.cnblogs.com/wucg/p/3106097.html
Copyright © 2011-2022 走看看