如何檢查瀏覽器或不ASP.NET(MVC)如何檢查瀏覽器或啓用的cookie不
6
A
回答
0
我這是怎麼啓用的cookie:
登錄期間做出特殊的cookie,在URL中添加?cookiecheck = true,並在URL中看到cookiecheck時,檢查是否存在特定的檢查cookie。如果沒有,cookies被禁用。
11
這裏是我的授權過濾器對我的登錄操作方法:
/// <summary>
/// Ensures that cookies are enabled.
/// </summary>
/// <exception cref="CookiesNotEnabledException" />
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class EnsureCookiesAttribute : FilterAttribute, IAuthorizationFilter
{
private readonly string _cookieName;
private readonly bool _specificCookie;
/// <summary>
/// The name of the cookie to use to ensure cookies are enabled.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible",
Justification = "Field is public so that the default value may be modified.")]
public static string DefaultCookieName = "SupportsCookies";
public const string CookieCheck = "cookieCheck";
/// <summary>
/// Checks to make sure cookies are generally enabled.
/// </summary>
public EnsureCookiesAttribute() : this(null) { }
/// <summary>
/// Checks to make sure a cookie with the given name exists
/// </summary>
/// <param name="cookieName">The name of the cookie</param>
public EnsureCookiesAttribute(string cookieName)
{
if (String.IsNullOrEmpty(cookieName))
{
cookieName = DefaultCookieName;
}
else
{
_specificCookie = true;
}
QueryString = CookieCheck;
_cookieName = cookieName;
}
/// <summary>
/// The name of the cookie to check for.
/// </summary>
public string CookieName
{
get { return _cookieName; }
}
/// <summary>
/// The querystring parameter to use to see if a test cookie has been set.
/// </summary>
public string QueryString { get; set; }
protected static CookiesNotEnabledException CreateBrowserException()
{
return new CookiesNotEnabledException("Your browser does not support cookies.");
}
protected static CookiesNotEnabledException CreateNotEnabledException()
{
return new CookiesNotEnabledException("You do not have cookies enabled.");
}
#region Implementation of IAuthorizationFilter
/// <summary>
/// Called when authorization is required.
/// </summary>
/// <param name="filterContext">The filter context.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes"
, Justification = "Should swallow exceptions if a cookie can't be set. This is the purpose of the filter.")]
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException("filterContext");
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
if (!request.Browser.Cookies)
throw CreateBrowserException();
string currentUrl = request.RawUrl;
var noCookie = (request.Cookies[CookieName] == null);
if (!_specificCookie && noCookie && request.QueryString[QueryString] == null)
{
try
{
// make it expire a long time from now, that way there's no need for redirects in the future if it already exists
var c = new HttpCookie(CookieName, "true") {Expires = DateTime.Today.AddYears(50)};
response.Cookies.Add(c);
currentUrl = currentUrl + (currentUrl.Contains('?') ? "&" : "?") + QueryString + "=true";
filterContext.Result = new RedirectResult(currentUrl);
return;
}
catch
{
}
}
if (noCookie)
throw CreateNotEnabledException();
}
#endregion
}
/// <summary>
/// Thrown when cookies are not supported.
/// </summary>
[Serializable]
public class CookiesNotEnabledException : HttpException
{
public CookiesNotEnabledException()
{
}
protected CookiesNotEnabledException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public CookiesNotEnabledException(string message)
: base(message)
{
}
public CookiesNotEnabledException(string message, Exception innerException)
: base(message, innerException)
{
}
}
你可以用它來確保啓用了Cookie作爲
[EnsureCookies]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="NoCookies")]
public ActionResult LogOn(....) ...
或者以確保特定的Cookie已設置爲一個動作
[EnsureCookies("MyCookie")]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="Some cookie not set view"]
public ActionResult ActionThatNeedsMyCookie()....
我不知道爲什麼你需要這樣做,但它有我秒。希望它有幫助。
相關問題
- 1. 如何檢查,如果瀏覽器在Web應用程序中啓用Cookie?
- 2. 檢查在IE瀏覽器啓用Java
- 3. 如何檢查瀏覽器的JavaScript是否啓用
- 4. 如何在瀏覽器中檢查cookie設置爲httponly
- 5. 如何使用jquery檢查瀏覽器?
- 6. 如何檢查Flash是否在瀏覽器中啓用?
- 7. 如何使用java檢索瀏覽器的所有cookie?
- 8. 如何在Delphi中爲僞瀏覽器啓用Cookie?
- 9. 使用Cookie檢測蜘蛛或瀏覽器
- 10. 在Chrome瀏覽器和/或Safari瀏覽器中啓用XSS
- 11. 檢查瀏覽器Cookie是否被禁用
- 12. 如何管理瀏覽器的Cookie
- 13. 如何使用瀏覽器檢測cookie和javascript
- 14. Cookie和瀏覽器
- 15. 檢查用戶何時啓動了Android瀏覽器
- 16. android webview或瀏覽器不刪除設備重啓時的會話cookie
- 17. 如何查找瀏覽器的cookies是啓用還是禁用?
- 18. 針對不同瀏覽器的Cookie
- 19. 檢查瀏覽器負載
- 20. URL檢查在瀏覽器
- 21. 在php中跨瀏覽器cookie。在不同瀏覽器之間傳遞Cookie
- 22. 如何在Delphi中引入前端瀏覽器或啓動瀏覽器
- 23. 如何使用Gulp服務啓動不同的瀏覽器或多個瀏覽器?
- 24. 檢查應用程序是否由網絡瀏覽器啓動
- 25. 檢查Java是啓用與否在IE8瀏覽器通過
- 26. 我如何使用IE瀏覽器(或不同的瀏覽器)爲每個瀏覽器控制?
- 27. 如何禁用瀏覽器瀏覽器?
- 28. 瀏覽器cookie問題
- 29. 閱讀瀏覽器Cookie ... jQuery
- 30. 瀏覽器cookie問題
不錯的解決方案;儘管如果異常處理程序試圖設置cookie並且重新讀取,它會很好。預防循環將是重要的.. – LamonteCristo 2011-11-07 17:52:07
你好!我得到你的代碼,然後我在這裏有Execption:'if(noCookie)throw CreateNotEnabledException();'如何處理它? – user3818229 2015-12-27 10:02:18
我是否需要重寫OnExecption方法? – user3818229 2015-12-27 10:03:30