需要在HttpContext的是什么,让FormsAuthentication.SignOut()来执行? [英] What is needed in the HttpContext to allow FormsAuthentication.SignOut() to execute?
问题描述
我想写我们注销方法的单元测试。除此之外它 FormsAuthentication.SignOut()
。然而,它抛出一个 System.NullReferenceException
。
I am trying to write a unit test for our log out method. Among other things it FormsAuthentication.SignOut()
. However, it throws a System.NullReferenceException
.
我创建了一个模拟; 的HttpContext
(使用MOQ),但它显然是失去了一些东西。
I've created a mock; HttpContext
(using Moq), but it is obviously missing something.
我的模拟上下文包含:
- 接着嘲笑
的Htt prequestBase
在请求
- 接着嘲笑
的Htt presponseBase
在响应
- 使用
HttpCookieCollection
,另一个是关于Response.Cookies
- 接着嘲笑
的IPrincipal
在用户
在
Request.Cookies时
- A mocked
HttpRequestBase
onRequest
- A mocked
HttpResponseBase
onResponse
- With a
HttpCookieCollection
onRequest.Cookies
and another onResponse.Cookies
- A mocked
IPrincipal
onUser
的我知道我可以去包装路线,并注入空 FormsAuth
包装对象,在它的地方,但我真的想避免3其他文件只是为了解决code的一条线。这一点,我还是好奇的答案的
I am aware I could go the wrapper route and inject an empty FormsAuth
wrapper object in it's place, but I would really like to avoid the 3 additional files just to fix one line of code. That and I am still curious for an answer
所以我的问题是需要在什么的HttpContext
允许 FormsAuthentication.SignOut()来执行。
So my question is "What is needed in the HttpContext
to allow FormsAuthentication.SignOut() to execute.
"
推荐答案
这里的code为signout。
Here's the code for signout.
public static void SignOut()
{
Initialize();
HttpContext current = HttpContext.Current;
bool flag = current.CookielessHelper.DoesCookieValueExistInOriginal('F');
current.CookielessHelper.SetCookieValue('F', null);
if (!CookielessHelperClass.UseCookieless(current, false, CookieMode) || current.Request.Browser.Cookies)
{
string str = string.Empty;
if (current.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
{
str = "NoCookie";
}
HttpCookie cookie = new HttpCookie(FormsCookieName, str);
cookie.HttpOnly = true;
cookie.Path = _FormsCookiePath;
cookie.Expires = new DateTime(0x7cf, 10, 12);
cookie.Secure = _RequireSSL;
if (_CookieDomain != null)
{
cookie.Domain = _CookieDomain;
}
current.Response.Cookies.RemoveCookie(FormsCookieName);
current.Response.Cookies.Add(cookie);
}
if (flag)
{
current.Response.Redirect(GetLoginPage(null), false);
}
}
看起来像你需要一个CookielessHelperClass实例。太遗憾了,内部的密封 - 有没有办法,除非你使用TypeMock嘲笑它。 +1包装建议:)
Looks like you need a CookielessHelperClass instance. Too bad it's internal and sealed - there's no way to mock it unless you're using TypeMock. +1 for wrapper suggestions :)
这篇关于需要在HttpContext的是什么,让FormsAuthentication.SignOut()来执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!