làm thế nào để kiểm tra cookie sẽ kích hoạt cho trình duyệt hay không trong ASP.NET (MVC)làm thế nào để kiểm tra cookie sẽ kích hoạt cho trình duyệt hay không
6
A
Trả lời
0
Làm thế nào tôi đã làm điều đó:
làm cho một cookie đặc biệt trong khi đăng nhập, add cookiecheck = true trong URL, và khi nhìn thấy cookiecheck trong url, kiểm tra xem một cookie đặc biệt cho kiểm tra có tồn tại hay không. nếu không, cookie sẽ bị vô hiệu hóa.
11
Dưới đây là bộ lọc cho phép tôi về phương pháp hành động đăng nhập của tôi:
/// <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)
{
}
}
Bạn có thể sử dụng nó để đảm bảo rằng các tập tin cookie được cho phép như
[EnsureCookies]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="NoCookies")]
public ActionResult LogOn(....) ...
hoặc để đảm bảo rằng một cookie cụ thể đã được thiết lập cho một hành động
[EnsureCookies("MyCookie")]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="Some cookie not set view"]
public ActionResult ActionThatNeedsMyCookie()....
Tôi không chắc chắn tại sao bạn cần làm điều đó, nhưng có nó tôi S. Hy vọng nó hữu ích.
Các vấn đề liên quan
- 1. Làm thế nào để kiểm tra, nếu trình duyệt đã bật cookie trong ứng dụng web?
- 2. C# Tràn không hoạt động? Làm thế nào để kích hoạt Kiểm tra tràn?
- 3. Máy chủ SQL: kiểm tra xem Trình kích hoạt có được bật hay tắt không?
- 4. Làm thế nào để kiểm tra xem một hoạt động đang chạy hay không
- 5. Làm thế nào để kích hoạt ng-thay đổi trong kiểm tra chỉ thị trong AngularJS
- 6. Khung kiểm tra nào cho Javascript hỗ trợ Kiểm tra mà không cần trình duyệt?
- 7. Trình kích hoạt mã Javascript hoặc trình kiểm tra
- 8. làm thế nào để kiểm tra mod_deflate được kích hoạt trong apache?
- 9. Tắt cookie trong trình duyệt Android không hoạt động
- 10. Làm thế nào để bạn kiểm tra các cookie AngularJS trong một bài kiểm tra đầu cuối?
- 11. Kiểm tra các trình kích hoạt DISABLE/ENABLE IN SQL
- 12. Làm cách nào để bạn kiểm tra cookie bằng Chrome?
- 13. SqlBulkInsert - Làm thế nào để thiết lập kích hoạt cháy, kiểm tra ràng buộc?
- 14. Làm cách nào để bật kiểm tra chính tả trình duyệt trong CKEditor 4?
- 15. Làm cách nào để kiểm tra trình duyệt hỗ trợ cảm ứng bằng JS/jQuery?
- 16. Làm thế nào để kích hoạt mod_rewrite?
- 17. Cách kiểm tra JavaScript của trình duyệt được bật hay không
- 18. Làm thế nào để kiểm tra mảng byte trống hay không?
- 19. RadioGroup: Làm thế nào để kiểm tra theo chương trình
- 20. Cách nhanh hơn và ngắn hơn để kiểm tra xem cookie có tồn tại hay không
- 21. Làm thế nào để gửi cookie với trình duyệt web selen?
- 22. Cách kiểm tra xem trình kích hoạt có hợp lệ không?
- 23. Trình duyệt nào hỗ trợ cookie HttpOnly?
- 24. Làm thế nào để kiểm tra xem một id email có tồn tại hay không?
- 25. Làm thế nào để kích hoạt xdebug với nginx?
- 26. Làm thế nào để bạn kích hoạt tính năng giám sát FPS để kiểm tra hoạt ảnh XAML?
- 27. Làm cách nào để kiểm tra xem một int không rỗng hay rỗng?
- 28. Truy vấn để kiểm tra xem trình kích hoạt có tồn tại trên bảng MYSQL
- 29. Làm thế nào để kích hoạt VsVim?
- 30. xóa cookie khỏi trình duyệt
Giải pháp tốt; mặc dù nó sẽ là tốt đẹp nếu xử lý ngoại lệ đã cố gắng để thiết lập các cookie, và đọc lại nó. Ngăn chặn các vòng lặp sẽ là quan trọng .. – LamonteCristo
Xin chào! Tôi đã nhận mã của bạn và sau đó tôi có Execption ở đây: 'if (noCookie) ném CreateNotEnabledException();' Làm thế nào để xử lý nó? – user3818229
Tôi có cần ghi đè phương pháp OnExecption không? – user3818229