如何检查浏览器或不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