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 = "用户名或密码错误";
    

      

  • 相关阅读:
    About Face 摘录
    断言的使用
    C#中值传递和引用传递
    C++技巧之断言Assert
    About Face 一 目标导向设计
    About Face 二 设计行为与形态
    C++中引用传递与指针传递区别
    一个新的时代SoLoMo
    离散数学笔记算法部分
    汪教授的离散数学20110308 谓词与量词2
  • 原文地址:https://www.cnblogs.com/wucg/p/3106097.html
Copyright © 2011-2022 走看看