Asp.net mvc ASP.NET MVC创建模拟用户
我有一个MVC应用程序,其中我有一个用户类,用户还可以模拟另一个用户(仅限管理员用户) 下面的代码验证了请求并实例化了我的用户类版本 然后,它尝试从会话对象获取模拟用户,但该会话在global.asax的此方法中不可用 希望这是有意义的 我还能怎么做 我想我的问题是,在global.asax方法中,对于每个请求,您在什么时候可以访问会话对象Asp.net mvc ASP.NET MVC创建模拟用户,asp.net-mvc,Asp.net Mvc,我有一个MVC应用程序,其中我有一个用户类,用户还可以模拟另一个用户(仅限管理员用户) 下面的代码验证了请求并实例化了我的用户类版本 然后,它尝试从会话对象获取模拟用户,但该会话在global.asax的此方法中不可用 希望这是有意义的 我还能怎么做 我想我的问题是,在global.asax方法中,对于每个请求,您在什么时候可以访问会话对象 protected void Application_OnAuthenticateRequest(object sender, EventArgs e) {
protected void Application_OnAuthenticateRequest(object sender, EventArgs e)
{
IMylesterService service = ObjectFactory.GetInstance<IMylesterService>();
if (Context.User != null)
{
if (Context.User.Identity.IsAuthenticated)
{
User user = service.GetUser(Context.User.Identity.Name);
if (user == null)
throw new ApplicationException("Context.user.Identity.name is not a recognized user");
User impersonatedUser = (User)this.Session["ImpersonatedUser"];
if (impersonatedUser == null)
user.ImpersonatedUser = user;
else
user.ImpersonatedUser = impersonatedUser;
System.Threading.Thread.CurrentPrincipal = Context.User = user;
return;
}
}
User guest = service.GetGuestUser();
guest.ImpersonatedUser = guest;
System.Threading.Thread.CurrentPrincipal = Context.User = guest;
}
受保护的无效应用程序\u OnAuthenticateRequest(对象发送方,事件参数e)
{
IMylesterService服务=ObjectFactory.GetInstance();
if(Context.User!=null)
{
if(Context.User.Identity.IsAuthenticated)
{
User=service.GetUser(Context.User.Identity.Name);
if(user==null)
抛出新的ApplicationException(“Context.user.Identity.name不是可识别的用户”);
User impersonatedUser=(User)this.Session[“impersonatedUser”];
if(impersonatedUser==null)
user.ImpersonatedUser=用户;
其他的
user.ImpersonatedUser=ImpersonatedUser;
System.Threading.Thread.CurrentPrincipal=Context.User=User;
返回;
}
}
用户guest=service.GetGuestUser();
guest.ImpersonatedUser=guest;
System.Threading.Thread.CurrentPrincipal=Context.User=guest;
}
在AuthenticateRequest期间,会话将不可用:您需要做的是将所需信息标记到Identity.userData;例如,如果使用表单身份验证,请执行以下操作:
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Context.User != null)
{
if (Context.User.Identity.IsAuthenticated)
{
// retrieve the value
var id = (FormsIdentity)Context.User.Identity;
var myvalue = id.Ticket.UserData; // "Here you are"
}
}
}
对于使用表单登录,您需要编写自定义cookie:
MVC->class FormsAuthenticationService:IFormsAuthenticationService
public static void SetAuthenticationCookie(HttpContextBase context, FormsAuthenticationTicket ticket)
{
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName)
{
Value = FormsAuthentication.Encrypt(ticket),
Secure = FormsAuthentication.RequireSSL,
Domain = FormsAuthentication.CookieDomain,
HttpOnly = true,
Expires = DateTime.Now.AddMinutes(15)
};
if (!context.Request.IsSecureConnection && FormsAuthentication.RequireSSL)
{
throw new HttpException("Ticket requires SSL.");
}
context.Response.Cookies.Add(cookie);
}
public static FormsAuthenticationTicket CreateTicket(HttpContextBase context, string emailAddress, string userData, bool persist)
{
return new FormsAuthenticationTicket(1, emailAddress, DateTime.Now, DateTime.Now.AddMinutes(15), persist, userData, FormsAuthentication.FormsCookiePath);
}
最后,在SignIn中,您现在可以通过调用CreateTicket(…)创建所需的票证,然后通过SetAuthenticationCookie(…)将其写出
我从未使用过它,但假设会话状态是在AcquisiteRequestState期间分配的:
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
我遇到了同样的问题,需要访问global.asax中的会话,最后通过将代码移动到
acquisitequeststate
处理程序中解决了这个问题,这是在通过身份验证后发生的
protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
if (Request.IsAuthenticated && Context.Session != null)
{
// access Context.Session
}
}
这会触发很多次,并且当前上下文并不总是具有有效的会话对象,因此需要进行检查
编辑:还必须添加对IsAuthenticated的检查——注销时收到空错误。现在效果很好。尝试创建授权筛选器:
public class CustomAuthorizationFilter : AuthorizeAttribute
{
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
// perform your authorization here
// full access to HttpContext and session
}
}
然后可以将此属性应用于控制器。理想情况下,您应该有一个所有其他控制器都继承自的基本控制器,并且可以在该控制器的类级别应用该属性。然后,您的所有请求都将得到授权,并按照上面的编码应用模拟。最近我遇到了这个问题,希望有人能插话。我唯一能开始工作的就是使用OnSessionStart,但是如果你当时没有通过身份验证,你就错失了机会。我正在移植一个ASP.NET应用程序,其中一些在共享基础ASPX中运行的会话代码现在需要转到其他地方。。。
public class CustomAuthorizationFilter : AuthorizeAttribute
{
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
// perform your authorization here
// full access to HttpContext and session
}
}