WebApi设置HttpContext.Current.User
1、Web.config配置上system.web节点下加入以下配置
<system.web>
<authentication mode="Forms">
<forms name=".wechat" loginUrl="url" timeout="30" protection="All" defaultUrl="/index.html" />
</authentication>
<httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>
2、上代码
using Newtonsoft.Json;
using System;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Security; namespace KMHC.CTMS.DrugStore.Controllers
{
public class BaseApiController : ApiController
{
protected void SignIn(dynamic user)
{
if (user == null)
return;
var httpContext = HttpContext.Current; var version = 1;
var name = user.OpenId;
var now = DateTime.Now.ToLocalTime();
var expiration = now.Add(TimeSpan.FromDays(30));
var isPersistent = true;
var userData = JsonConvert.SerializeObject(user); var ticket = new FormsAuthenticationTicket(version, name, now, expiration, isPersistent, userData, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Path = FormsAuthentication.FormsCookiePath
};
cookie.Expires = ticket.Expiration;
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
} var url = HttpContext.Current.Request.Url.ToString();
if (!string.IsNullOrEmpty(url) && url.StartsWith("https"))
{
cookie.Secure = true;
} httpContext.Response.Cookies.Add(cookie); httpContext.User = new GenericPrincipal(new FormsIdentity(ticket), new string[] { "Wechat" });
} protected string OpenId
{
get
{
return this.User.Identity.Name;
}
} protected bool IsAuthenticated
{
get
{
return this.User.Identity.IsAuthenticated;
}
} protected dynamic UserInfo {
get {
var identity = (FormsIdentity)HttpContext.Current.User.Identity;
var user = JsonConvert.DeserializeObject<dynamic>(identity.Ticket.UserData);
return user;
}
}
}
}
3、默认地址获取
//index.html <forms name=".wechat" loginUrl="url" timeout="30" protection="All" defaultUrl="/index.html" />
var defaultUrl= FormsAuthentication.DefaultUrl;
WebApi设置HttpContext.Current.User的更多相关文章
- 异步任务,HttpContext.Current为null解决办法
		
最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task ...
 - .net webapi 中使用session是出错  HttpContext.Current.Session==null
		
最近在写.net webapi时发现 HttpContext.Current.Session==null ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...
 - System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")  未将对象引用设置为实例对象
		
做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 获取路径本来这个方法用的好好的 因为需要 ...
 - 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题
		
今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...
 - HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。
		
多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...
 - HttpContext.Current.Session[strName]未将对象引用设置到对象的实例
		
项目开发是在4.5.1上,不知道为啥客户提供的服务器上安装的是4.5,差别不大也没去升级,然后部署MVC的时候web.config报错 <system.web> <compilati ...
 - HttpContext.Current.Server.MapPath("") 未将对象设置到引用的
		
在多线程中使用该方法获取目录报错:未将对象设置到引用 #region 上传图片到腾讯 public async Task<Result> UploadImageToWX(string ba ...
 - 异步 HttpContext.Current 为空null 另一种解决方法
		
1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...
 - WebApi设置SessionState为Required
		
public override void Init() { //在注册管道事件中 require session state //只能在引发“HttpApplication.AcquireReques ...
 
随机推荐
- DAY1_PYTHON基础作业
			
''' print("1.使用while循环输入 1 2 3 4 5 6 8 9 10") count = 1 while count < 11: print(count) ...
 - WEB学习笔记14-HTML5新特性的使用
			
(1)定义文档类型声明 <!DOCTYPE html> (2)定义页面编码 <!—HTML 4.01中定义的设置页面编码的方式--> <meta http-equiv=& ...
 - WEB学习笔记3-开发环境和工具
			
WEB前端集成开发环境:Aptana Studio和WebStormWEB前端代码调试:IE浏览器自带的IE Dev Toolbar,Chrome浏览器自带的Developer Tools,Firef ...
 - 华莱士的 第二个python程序之(格式化输出)
			
name = input("name:")age = input("age:")info ='''---------- info of %s --------- ...
 - Linux 安装aria2
			
Ubuntu/Linux Mint/Debian sudo apt-get install aria2 CentOS/Fedora sudo yum install aria2 Manjaro/Arc ...
 - java数组的定义
			
class ArrayDome { public static void main(String[] args) { //元素类型[] 数组名 创建一个 元素类型[元素个数或数组长度] /* 需要一个 ...
 - [LeetCode&Python] Problem 703. Kth Largest Element in a Stream
			
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
 - android 判断横竖屏的方法(转)
			
public boolean isScreenChange() { Configuration mConfiguration = this.getResources().getConfiguratio ...
 - oc语言的Foundation框架(学习笔记2)
			
紧接上文…… 4.集合对象 4.1数组 1.基本概念 Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取到数组中的各个元素,也分可变和不 ...
 - Django_ORM字段_字段参数
			
Object Relational Mapping (ORM) ORM:对象关系映射模式是一种为解决面向对象与关系数据库存在的互补匹配的现象技术.简单说就是通过使用描述对象和数据库之间的映射的元数据, ...