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的更多相关文章

  1. 异步任务,HttpContext.Current为null解决办法

    最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task ...

  2. .net webapi 中使用session是出错 HttpContext.Current.Session==null

    最近在写.net webapi时发现 HttpContext.Current.Session==null  ,导致报错,后来查资料发现webapi中使用session时首先需要开启session功能, ...

  3. System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象

    做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")   获取路径本来这个方法用的好好的 因为需要 ...

  4. 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题

    今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...

  5. HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。

    多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...

  6. HttpContext.Current.Session[strName]未将对象引用设置到对象的实例

    项目开发是在4.5.1上,不知道为啥客户提供的服务器上安装的是4.5,差别不大也没去升级,然后部署MVC的时候web.config报错 <system.web> <compilati ...

  7. HttpContext.Current.Server.MapPath("") 未将对象设置到引用的

    在多线程中使用该方法获取目录报错:未将对象设置到引用 #region 上传图片到腾讯 public async Task<Result> UploadImageToWX(string ba ...

  8. 异步 HttpContext.Current 为空null 另一种解决方法

    1.场景 在导入通讯录过程中,把导入的失败.成功的号码数进行统计,然后保存到session中,客户端通过轮询显示状态. 在实现过程中,使用的async调用方法,出现HttpContext.Curren ...

  9. WebApi设置SessionState为Required

    public override void Init() { //在注册管道事件中 require session state //只能在引发“HttpApplication.AcquireReques ...

随机推荐

  1. MySQL之内连接、左连接和右连接

    数据表内数据如下: books表:                                                                                    ...

  2. 虚拟机中ubuntu不能联网问题的解决——NAT方式

    困惑我多时的Ubuntu联网问题终于解决啦,开心!!!现记录如下,方便日后取用. 可先直接尝试第3步,若不行,则走完全程. 1.查看/设置下NAT的网络 打开VMware Workstation, 点 ...

  3. 安装mingw编译器

    1.下载地址:https://sourceforge.net/projects/mingw/files/latest/download?source=top3_dlp_t5 2. 安装下载的(ming ...

  4. 浅谈MVC中的service层(转)

    概述 mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层 ...

  5. LeetCode第一次刷题

    感觉自身编程水平还是差很多,所以刷刷题 LeetCode貌似是一个用的人比较多的题库,下面是第一题 给数组和目标和求需要元素的下标 public class Solution { public int ...

  6. bug狩猎

    最近需求少了,终于有时间修一下底层的bug,做一点工具方便查bug,写篇文章简单记录一下. 一是优化了一个玩法进程的启动速度.这个玩法需要用战力做匹配,玩家按战力分段放进不同的桶里,每个桶用skipl ...

  7. 前端浏览器自动刷新神器:Browsersync

    [安装] 1 npm install -g browser-sync [静态项目使用browsersync] 自己可以去browsersync官网查看,其实使用很简单,总结下就是: 1 browser ...

  8. Android IPC机制基础

    概要 多进程概念及多进程常见注意事项 IPC基础:Android序列化和Binder 跨进程常见的几种通信方式:Bundle通过Intent传递数据,文件共享,ContentProvider,基于Bi ...

  9. vs2015打开Dialog出现HRESULT:0x8CE0000B

    关闭项目  在工程目录找到.vc.db文件删除

  10. 下载python包

    修改pip下载源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider 更新pip版本 python -m pip inst ...