using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace pdf2word.Areas.User
{
public class UserAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectResult(
string.Concat("/user/Account/Login", "?ReturnUrl=", filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString())));
}
}
private bool CookieIsValid(HttpCookie cookie)
{
if (cookie == null) return false;
try
{
FormsAuthenticationTicket ticketback = FormsAuthentication.Decrypt(cookie.Value);
if (String.IsNullOrEmpty(ticketback.Name))
return false;
else
return true;
}
catch
{
return false;
}
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
return false;
if (CookieIsValid(httpContext.Request.Cookies["cokiename"]))
return true;
else
{
httpContext.Response.StatusCode = 403;
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace pdf2word.Areas.User
{
public class UserAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectResult(
string.Concat("/user/Account/Login", "?ReturnUrl=", filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString())));
}
}
private bool CookieIsValid(HttpCookie cookie)
{
if (cookie == null) return false;
try
{
FormsAuthenticationTicket ticketback = FormsAuthentication.Decrypt(cookie.Value);
if (String.IsNullOrEmpty(ticketback.Name))
return false;
else
return true;
}
catch
{
return false;
}
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
return false;
if (CookieIsValid(httpContext.Request.Cookies["cokiename"]))
return true;
else
{
httpContext.Response.StatusCode = 403;
return false;
}
}
}
}