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. erlang转化中文为url

    今天使用http get 方法时,参量中有中文而导致出错. 例如http://abc.com/abc?arg=中文,在erlang使用http:request方法失败. 后来查了url的规范,url中 ...

  2. OSG学习:阴影代码示例

    效果图: 代码示例: #include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #i ...

  3. 敏捷冲刺DAY6

    一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 对于可视控件,是能进行设计的,但是对于不可视组件,比如AdoConnection怎么才能设计.但是我看del ...

  4. 【leetcode】62.63 Unique Paths

    62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...

  5. 【转】MySQL数据类型

    1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) mediumint(m) ...

  6. webgl 初识2

    之前的文章介绍了webgl. 这里进一步精简. WebGL的全部内容就是创建不同的着色器, 向着色器提供数据然后调用gl.drawArrays 或 gl.drawElements 让WebGL调用当前 ...

  7. CF44H Phone Number

    题意翻译 给你一个电话号码,根据这个号码生成一个新的号码.生成的规则就是 新号码的第一个数任意选(0-9), 然后之后的每一个新号码都按照以下规则生成: 第i个新号码=(第i-1个新号码+第i个老号码 ...

  8. 【BZOJ1565】【NOI2009】植物大战僵尸(网络流)

    [BZOJ1565][NOI2009]植物大战僵尸(网络流) 题面 BZOJ 洛谷 题解 做了这么多神仙题,终于有一道能够凭借自己智商能够想出来的题目了.... 好感动. 这就是一个比较裸的最小割模型 ...

  9. Jenkins远程代码执行漏洞检查(CVE-2017-1000353)

    Jenkins的反序列化漏洞,攻击者使用该漏洞可以在被攻击服务器执行任意代码,漏洞利用不需要任何的权限 漏洞影响范围: 所有Jenkins主版本均受到影响(包括<=2.56版本)所有Jenkin ...

  10. NOIP2015普及组T4推销员(暴力+线段树)

    题目:阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有N家住户,第i家住户到入口的距离为Si米.由于同一栋房子里 ...