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 ...
随机推荐
- swiper遇到的问题
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- 2019 Power BI最Top50面试题,助你面试脱颖而出系列<下>
Q:什么是附加题? A:这就是常说的送分可选题,可做可不做:也可以说是加分项,是难点提升题. Power BI 面试题 — 附加题 33)什么是 Power View? 答案: Power View是 ...
- Flyweight 享元(结构型)
一:描述:(该模式实际应用较少) Flyweight 享元模式是对大量细粒度的元素进行共享和重用.减少对象的创建减轻内存: 注和单例模式不同的是:享元模式的各个对象佣有各自的行为并可实例化,单例模式的 ...
- Spark读HBase写MySQL
1 Spark读HBase Spark读HBase黑名单数据,过滤出当日新增userid,并与mysql黑名单表内userid去重后,写入mysql. def main(args: Array[Str ...
- CentOS下将php和mysql命令加入到环境变量中-简单
开发过程中.需要使用到php命令执行程序.但是php命令没有在全局命令中:每次执行都需要加上全路径特别麻烦,把php命令添加到全局变量中,以后每次只用输入php可以了 例: php -v 或 mys ...
- JavaSE-基本数据类型
一些基础: 字节与字符.字节与位:1个字节=8位(bit)(最高位是符号位,0正数1负)bit是电脑记忆体中最小的单位,在二进位电脑系统中,每一bit 可以代表0 或 1 的数位讯号.bps 是 bi ...
- PA教材提纲 TAW10-2
Unit1 Introduction to the ABAP Dictionary(ABAP字典介绍) 1.1 Describing the ABAP Dictionary(描述ABAP字典) ABA ...
- PA教材提纲 TAW10-1
Unit1 SAP systems(SAP系统) 1.1 Explain the Key Capabilities of SAP NetWeaver(解释SAP NetWeaver的关键能力) Rep ...
- 从零开始在iPhone上运行视频流实时预测模型应用,只需10步
1、买一台苹果电脑,建议MacBook Pro. 2、安装Xcode. 3、克隆TensorFlow:https://github.com/tensorflow/tensorflow.git 4、下载 ...
- ssh_exchange_identification: read: Connection reset by peer
vim /etc/hosts.deny 删除ip 借鉴:https://www.sunnyos.com/article-show-81.html