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 ...
随机推荐
- vue 获取当前时间
timeNow () { return moment().utc().format('YYYY年MM月DD日') + ' ' + moment().utc().format('dddd')}
- tmux 没有默认配置文件。
解决: 1.复制粘贴一个(有例子),然后关闭tmux server. tmux kill-server.重新启动生效. 2.复制粘贴一个,然后sourece一下: tmux source ~/ ...
- 2017年java面试题【集合篇】
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征. 这里有10个经典的Java面试题,也为大 ...
- 【Linux】comm指令
语法 comm [-123][--help][--version][第1个文件][第2个文件] 参数 -1 不显示只在第1个文件里出现过的列 -2 不显示只在第2个文件里出现过的列 -3 不显示只在第 ...
- H5中input[type="date"]默认样式修改 伪类
- The listener supports no services
问题描述 sql> lsnrctl start 提示: The listener supports no services The command completed successfully ...
- 简述grub启动引导程序配置及命令行接口详解
一.版本 grub:Grand Unified Bootloader grub 0.x:grub legacy grub 1.x:grub2 二.grub legacy 三个过程 stage1:安装在 ...
- Classloader精简重点
如果想学习classloader的具体内容,请goodu JVM 在运行时会产生三个ClassLoader,Bootstrap ClassLoader.Extension ClassLoader和 A ...
- Vue Affix组件
在vue的项目中经常用到固钉,但是 element-ui 上并没有提供这样的组件可供使用,ant-design-vue 有提供,总不能为了这一个组件再去引入一个组件库吧 下面是一个封装好的 affix ...
- 部署JupyterLab和pyalgotrade搭建web策略回测环境
==========================================================================安装anaconda 3 64位版本cd /optm ...