tarayıcısı için ya da değil ben yaptım nasıltarayıcı veya için etkin çerez nasıl kontrol edileceğini değil
6
A
cevap
0
ASP.NET (MVC) etkinleştirilmiş çerez nasıl kontrol edileceğini:
giriş sırasında özel bir kurabiye yapmaya karar verdik, URL'de cookiecheck = true ekleyin ve URL'de cookiecheck'i görerek, kontrol için özel bir çerez olup olmadığını kontrol edin. değilse, çerezler devre dışı bırakılır.
11
İşte benim yetkilendirme filtre benim giriş eylem yöntemine var:
/// <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)
{
}
}
Çerezleri etkin olduğundan emin olmak için kullanabilirsiniz
[EnsureCookies]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="NoCookies")]
public ActionResult LogOn(....) ...
olarak veya belirli bir çerezi olmuştur emin olmak için Bir eylem için ayarla
[EnsureCookies("MyCookie")]
[HandleError(ExceptionType = typeof(CookiesNotEnabledException), View="Some cookie not set view"]
public ActionResult ActionThatNeedsMyCookie()....
Bunu neden yapmanız gerektiğinden emin değilim, ama işte s. Umarım yardımcı olur.
İlgili konular
- 1. umbraco'da çerez nasıl kontrol edilir?
- 2. yalnızca subversion olarak değiştirilmiş dosyaların nasıl kontrol edileceğini kontrol edin
- 3. Tarayıcı sonraki istekte çerez göndermiyor
- 4. Bir çerez olup olmadığını nasıl kontrol edersiniz?
- 5. SSL etkin sitesinde Çerez yerine JWT'yi kullanma
- 6. haritalama etkisi etkin değil
- 7. Wi-Fi'nin etkin olup olmadığını kontrol etme Android'de değil
- 8. WiFi durumu etkin değil
- 9. HTML5 Tarih desteklenen olmadığını algıla veya i kontrol edebilirsiniz değil
- 10. nasıl kontrol edileceğini modifiye fonksiyon formallerin şeyleri kırmak için benim bitmeyen arayışı içinde
- 11. Çerez tabanlı veya facebook kullanıcısını doğrulamak için Shiro nasıl kullanılır?
- 12. Hata ayıklayıcı aracı etkin değil
- 13. Bir div var nasıl kontrol edilir veya değil?
- 14. Firebase analytics UygulamaModemeniz etkin değil
- 15. gerçek tarayıcı veya herhangi bir bot veya paletli
- 16. Django'da bir çerez nasıl güncellenir
- 17. Android ile Zbar: Tarayıcı kamera viewport, tarayıcıda URL'yi gösterdikten sonra etkin değil ve siyah kalıyor
- 18. Css değerinin tarayıcı tarafından desteklenip desteklenmediğini nasıl kontrol edebilirim?
- 19. nasıl Tarayıcı Tarayıcı Kullanımı `
- 20. Kontrol veya JavaScript ile devre dışı bırak Tarayıcı Geri Düğmesi veya JavaScript ile angularjs
- 21. Mysql Workbench bağlantısı ssl etkin değil
- 22. Tarayıcı penceresini Javascript ile kontrol etme
- 23. kontrol her üç sütun ise değil ya null veya boş
- 24. PDO: Bağlantının etkin olup olmadığını nasıl kontrol edelim, gerçek mi?
- 25. SVN: etkin kullanıcısı kontrol edilen depoda değiştiriliyor
- 26. Açısal 2 atma hatası: Çıkış etkin değil
- 27. Köşeli UI yönlendirici üst durumu etkin değil
- 28. URLConnection'da çerez nasıl gönderilir?
- 29. NSUserDefaults Değer İçeriyor Veya Değil?
- 30. Excel ActiveX Listbox açık dosyada etkin değil
Güzel çözüm; istisna işleyici, çerezi kurmaya teşebbüs etse iyi olur ve onu okur. Döngüleri önlemek önemli olacaktır .. – LamonteCristo
Merhaba! Kodunuzu aldım ve sonra burada Execption var: 'if (noCookie) CreateNotEnabledException();' Nasıl davranılır? – user3818229
OnExecption yöntemini geçersiz kılmalı mıyım? – user3818229