请教一个.net webapi的问题,我用actionfilter获取header解析出user,然后把当前user放到httpcontext.current.user,action中调用获取数据的方法,在获取数据的类里边读取httpcontext.current.user并根据此user查询对应的数据返回。当我用postman进行测试的时候,同时先后发起两个相同路径的请求(不同用户)结果两个请求返回的数据总是一样,按逻辑上不同user返回的数据是不同的,不知道什么原因,请教哪位大神有没有碰到相同的情形,该如何解决,此问题已经困扰我一个星期了!!!跪求帮助
解决方案 »
- 晕,都变形儿了
- 遇到的关于SQL问题
- "不重新发送信息,则无法刷新网页",看了之前的贴,但是不能解决问题,还望各位老鸟鼎力相助!!!
- Asp.NET + Oracle 出现下面的错误,指定的参数已超出有效值的范围。参数名: 小时、分和秒参数描述无法表示的 DateTime。请各位大侠出手。
- 在datagrid中如何格式化日期?
- 大家帮忙来写个SQL增加字段的命令!谢谢了
- 请问oracle的longraw是和微软的image的类型一样的吗?
- 如何定义一个全局字符串?
- 关于框架的问题!!代码如下!想实现点击mani.aspx上的按钮,将main.aspx全屏显示,也就是将<frameset>的rows属性赋值为"100%,*",不知道
- dataGrid某单元格的响应事件(没搜索到合适的)
- print.Attributes.Add("disabled ", "disabled");上面加了判断状态,合同打印按钮就灰掉了?
- webform弹出模态对话框gridview丢失easyui-datagrid样式
{
//private static IPrincipal _principal;
//private static string applicationID;
/// <summary>
/// Get/Set 当前用户安全主体对象
/// </summary>
/// <res>
/// 即支持System.Threading也支持HttpContext,可用于ASP.NET环境中,也可以用非ASP.NET环境中
/// </res>
public static IPrincipal User
{
get
{
if (HttpContext.Current != null)
{
return HttpContext.Current.User;
}
else if (Thread.CurrentPrincipal != null)
{
return Thread.CurrentPrincipal;
}
else
{
return new Security.UnauthenticatedPrincipal();
}
}
set
{
if (HttpContext.Current != null)
{
HttpContext.Current.User = value;
}
Thread.CurrentPrincipal = value;
}
}
}
在每次请求的过滤器中都设置user为当前请求的user,当在数据访问类中读取user时,竟然只能读取到一个??
版权信息:版权所有(C) 2010,Hopson Corporation
系统名称:BasicInfo
作 者:Machine
完成日期:2018/12/13 14:14:08
内容摘要:实体类 BProjectController
以下代码为机器生成,不允许直接进行修改,如要扩展,请另建代码文件并以相同文件名+‘_Exp’为新的文件名。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using Hopson.DreamOne.Framework;
using Hopson.Framework.Utils;
using Hopson.Framework.Common;
using Hopson.DreamOne.Framework.BLL;
using Hopson.DreamOne.Framework.Entity;
using Hopson.Framework.Webapi.Controllers;
using Hopson.Framework.Webapi.WebUtility;
using Hopson.Framework.Webapi.Models;
using Hopson.DreamOne.Framework.BasicInfo.Entity;
using Hopson.DreamOne.Framework.BasicInfo.BLL;
using Hopson.DreamOne.Framework.BasicInfo.Entities;namespace HjCloud.Webapi.V1.Controllers
{
/// <summary>
/// Webapi Controller类 BProjectController
/// </summary>
public partial class BProjectController : ApiControllerBase
{
protected BProjectBLL bll; public BProjectController()
{
bll = new BProjectBLL(); } /// <summary>
/// 查询指定id的对象
/// GET api/bprojectentity/5
/// </summary>
/// <returns>ApiResult对象</returns>
//[AllowAnonymous]
[HttpGet]
public HttpResponseMessage Get(string id)
{
BProjectEntity obj = bll.GetEntity(id);
return JsonHelper.ScriptSerializeApiResponse<BProjectEntity>(obj);
} ///// <summary>
///// 获取项目
///// </summary>
///// <returns>ApiResult对象</returns>
//[AllowAnonymous]
//[HttpGet]
//public HttpResponseMessage GetProject(string PartnerID, string ProjectName)
//{
// QueryEntity qe = new QueryEntity();
// qe.addWhereEntity("PartnerID", CompareTypeEnum.Equal, PartnerID);
// qe.addWhereEntity("ProjectName", CompareTypeEnum.Equal, ProjectName);
// var projList = bll.GetEntitiesWithoutPermission(qe).ToList();
// var projectId = "";
// if (projList.Count > 0)
// projectId = projList.First().ProjectID;
// else
// projectId = "";
// return JsonHelper.ScriptSerializeApiResponse<string>(projectId);
//} /// <summary>
/// 获取所有数据
/// </summary>
/// <returns></returns>
//[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetAll()
{
IList<BProjectEntity> objList;
objList = bll.GetEntities().GetList();
return JsonHelper.ScriptSerializeApiResponse<IList<BProjectEntity>>(objList);
} /// <summary>
/// 查询数量
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
//[AllowAnonymous]
[HttpGet]
public HttpResponseMessage Count(string filter="")
{
QueryEntity query = new QueryEntity();
IList<QueryWhereEntity> qWhereList = StringUtils.ConvertFilter<BProjectEntity>(filter, new BProjectEntity());
foreach (QueryWhereEntity where in qWhereList)
{
query.WhereEntities.Add(where);
}
int total = bll.GetEntitiesCount(query);
return JsonHelper.ScriptSerializeApiResponse<int>(total);
} /// <summary>
/// 分页获取数据
/// GET api/bprojectentity?filter=""&page=1&pageSize=10&orderBy=""
/// </summary>
/// <param name="filter">过滤条件,前端通过QueryObjects来生成过滤字符串</param>
/// <param name="page">要获取数据的页码</param>
/// <param name="pageSize">每页数据条数</param>
/// <param name="orderBy">排序</param>
/// <returns>ApiResult对象</returns>
//[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetPage(string filter="",int page=1,int pageSize=10,string orderBy="")
{
IList<BProjectEntity> objList;
int total = bll.GetEntities(filter, page, pageSize, orderBy, out objList);
return JsonHelper.ScriptSerializeApiResponse<IList<BProjectEntity>>(objList);
} /// <summary>
/// 创建对象
/// POST api/bprojectentity
///</summary>
/// <returns>ApiResult对象</returns>
[HttpPost]
public HttpResponseMessage Create([FromBody] BProjectEntity model)
{
var msg = new ApiResult();
BProjectEntity obj = new BProjectEntity();
obj.CopyFrom(model);
obj.ValidationRules.CheckRules();
var errors = from r in obj.BrokenRulesCollection
where r.Severity == Hopson.Framework.Validation.RuleSeverity.Error
select r;
string message = "";
foreach(var item in errors)
{
message += string.Format("{0}:{1}", item.Property, item.Description);
}
if(!string.IsNullOrEmpty(message))
{
msg.errcode = ReturnCode.数据校验失败;
msg.errmsg = message;
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
}
string id = bll.InsertEntity(obj);
if(string.IsNullOrEmpty(id))
{
msg.errcode = ReturnCode.失败;
msg.errmsg = "对象创建失败";
}
else
{
msg.errcode = ReturnCode.成功;
msg.data = bll.GetEntity(id);
}
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
} /// <summary>
/// 修改对象
/// PUT api/bprojectentity/5
/// </summary>
/// <returns>ApiResult对象</returns>
[HttpPut]
public HttpResponseMessage Update(string id, [FromBody] BProjectEntity model)
{
var msg = new ApiResult();
string message = "";
var ret = bll.GetEntity(id);
if(ret == null)
{
message = string.Format("对象 id={0} 没有找到", id);
msg.errcode = ReturnCode.未找到对象;
msg.errmsg = message;
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
}
ret.CopyFrom(model);
ret.ValidationRules.CheckRules();
var errors = from r in ret.BrokenRulesCollection
where r.Severity == Hopson.Framework.Validation.RuleSeverity.Error
select r;
foreach (var item in errors)
{
message += string.Format("{0}:{1}", item.Property, item.Description);
}
if (!string.IsNullOrEmpty(message))
{
msg.errcode = ReturnCode.数据校验失败;
msg.errmsg = message;
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
}
int row = bll.UpdateEntity(ret);
if (row==0)
{
msg.errcode = ReturnCode.失败;
msg.errmsg = "对象更新失败";
}
else
{
msg.errcode = ReturnCode.成功;
msg.data = ret;
}
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
} /// <summary>
/// 删除对象
/// DELETE api/bprojectentity/5
/// </summary>
/// <returns>ApiResult对象</returns>
[HttpDelete]
public HttpResponseMessage Delete(string id)
{
var msg = new ApiResult();
int ret = bll.DeleteEntity(id);
string message = "";
if (ret==0)
{
message = string.Format("对象 id= {0} 不存在或删除失败", id);
msg.errcode = ReturnCode.失败;
msg.errmsg = message;
}
else
{
message = string.Format("对象 id= {0} 已经删除", id);
msg.errcode = ReturnCode.成功;
msg.data = message;
}
return JsonHelper.ScriptSerializeResponse<ApiResult>(msg);
}
}
}
这是我的api controller