using System;
using System.Web;
using System.Web.Security;
 
namespace AuthTest
{
  public class Authentication
  {
    /// <summary>
    /// 设置用户登陆成功凭据(Cookie存储)
    /// </summary>
    /// <param name="UserName">用户名</param>
    /// <param name="PassWord">密码</param>
    /// <param name="Rights">权限</param>
    public static void SetCookie(string UserName,string PassWord,string Rights)
    {
      //
      //String PassWord="test";
      //
      String UserData = UserName + "#" + PassWord+"#"+Rights;
      if (true)
      {
        //数据放入ticket
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), false, UserData);
        //数据加密
        string enyTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
    /// <summary>
    /// 判断用户是否登陆
    /// </summary>
    /// <returns>True,Fales</returns>
    public static bool isLogin()
    {
      return HttpContext.Current.User.Identity.IsAuthenticated;
    }
    /// <summary>
    /// 注销登陆
    /// </summary>
    public static void logOut()
    {
      FormsAuthentication.SignOut();
    }
    /// <summary>
    /// 获取凭据中的用户名
    /// </summary>
    /// <returns>用户名</returns>
    public static string getUserName()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length != 0)
        {
          return UserData[0].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
    /// <summary>
    /// 获取凭据中的密码
    /// </summary>
    /// <returns>密码</returns>
    public static string getPassWord()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length!=0)
        {
          return UserData[1].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
    /// <summary>
    /// 获取凭据中的用户权限
    /// </summary>
    /// <returns>用户权限</returns>
    public static string getRights()
    {
      if (isLogin())
      {
        string strUserData = ((FormsIdentity)(HttpContext.Current.User.Identity)).Ticket.UserData;
        string[] UserData = strUserData.Split('#');
        if (UserData.Length!=0)
        {
          return UserData[2].ToString();
        }
        else
        {
          return "";
        }
      }
      else
      {
        return "";
      }
    }
  }
}

使用Forms Authentication的更多相关文章

  1. Nancy之Forms authentication的简单使用

    一.前言 想必大家或多或少都听过微软推出的ASP.NET Identity技术,可以简单的认为就是一种授权的实现 很巧的是,Nancy中也有与之相类似的技术Authentication,这两者之间都用 ...

  2. Nancy 学习-身份认证(Forms authentication) 继续跨平台

    开源 示例代码:https://github.com/linezero/NancyDemo 上篇讲解Nancy的Basic Authentication,现在来学习Nancy 的Forms身份认证. ...

  3. ASP.NET 4.0 forms authentication issues with IE11

    As I mentioned earlier, solutions that rely on User-Agent sniffing may break, when a new browser or ...

  4. Forms Authentication in ASP.NET MVC 4

    原文:Forms Authentication in ASP.NET MVC 4 Contents: Introduction Implement a custom membership provid ...

  5. Forms Authentication and Role based Authorization: A Quicker, Simpler, and Correct Approach

    https://www.codeproject.com/Articles/36836/Forms-Authentication-and-Role-based-Authorization Problem ...

  6. An Overview of Forms Authentication (C#)

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/introduction/an-o ...

  7. .net core 共享 .Net Forms Authentication cookie

    Asp.net 项目迁移到 asp.net core 项目后需要 兼容以前老的项目的登录方式. Forms Authentication cookie 登录. 从网上搜集到关于这个问题的解决思路都没有 ...

  8. ASP.NET Session and Forms Authentication and Session Fixation

    https://peterwong.net/blog/asp-net-session-and-forms-authentication/ The title can be misleading, be ...

  9. Forms authentication timeout vs sessionState timeout

    https://stackoverflow.com/questions/17812994/forms-authentication-timeout-vs-sessionstate-timeout Th ...

  10. SSRS 2016 Forms Authentication

    SSRS 2016 comes with completely new report manager web interface and implementing form authenticatio ...

随机推荐

  1. MySQL的并发访问控制(锁)

    前言:任何的数据集只要支持并发访问模型就必须基于锁机制进行访问控制 锁种类 读锁:共享锁,允许给其他人读,不允许他人写写锁:独占锁, 不允许其他人读和写 锁类型 显示锁:用户手动请求读锁或写锁隐式锁: ...

  2. php自带的filter过滤函数

    PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. filter_has_var()检查是否存在指定输入类型的变量. filter_id()返回指定过滤器的 ID 号. filt ...

  3. js中如何获取页面的Url,域名和端口号

    有时候通过获取上个页面的Url来做一个跳转,获取域名防止非正常访问 获取上一个页面的一个URL,这个URL一般做一个页面的跳转 window.location.href <script>w ...

  4. 利用SSH secure Shell实现windows与linux之间传输文件

    在windows下安装SSH secure Shell.默认安装后有两个快捷方式. linux下需要安装openssh-server utuntu默认安装了opens是-client,所以不需要安装, ...

  5. hadoop跑第一个实例过程

    第一次跑hadoop实例,中间经过了不少弯路,特此记录下来: 第一步:建立一个maven过程,pom.xml文件:(打包为jar包) <dependency> <groupId> ...

  6. floyd最短路

    floyd可以在O(n^3)的时间复杂度,O(n^2)的空间复杂度下求解正权图中任意两点间的最短路长度. 本质是动态规划. 定义f[k][i][j]表示从i出发,途中只允许经过编号小于等于k的点时的最 ...

  7. 转: 解决【Unable to make the session state request to the session state server】

    错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...

  8. Spring Boot系列教程五:使用properties配置文件实现多环境配置

    一.前言 实际项目开发过程中会用到多个环境,比如dev,test,product环境,不同的环境可能使用不同参数,为便于部署提高效率,本篇主要通过properties配置文件来实现多环境的配置. 二. ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)

    A 每次可以换一个或不换,暴力枚举位置即可 B 模拟 C 二分答案.. 边界可以优化r=totb/(tota-p),二分可以直接(r-l>=EPS,EPS不要太小,合适就好),也可以直接限定二分 ...

  10. 洛谷 P1013 进制位 【搜索 + 进制运算】

    题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E KL KK E E ...