C# Asp.NETMVC中的Cookie Munging
我在谷歌上搜索了很多,但没有找到任何好东西来解释为什么我会大嚼饼干。C# Asp.NETMVC中的Cookie Munging,c#,asp.net,asp.net-mvc,cookies,C#,Asp.net,Asp.net Mvc,Cookies,我在谷歌上搜索了很多,但没有找到任何好东西来解释为什么我会大嚼饼干。 我知道在人渣的情况下,当 有些浏览器不支持cookie 有时用户在浏览器中禁用cookie 但是我对它没有什么疑惑。我想知道它在内部是如何工作的,以及它在Asp.NETMVC中是如何使用的。此外,它还有任何局限性,也有任何咀嚼饼干的替代品。 请给我一个更好的链接,让我简单地理解它。 谢谢。默认情况下,asp.net使用cookies存储登录用户的会话id。但每个浏览器中都有一个禁用cookie的选项。如果Cookie被禁用
我知道在人渣的情况下,当
- 有些浏览器不支持cookie
- 有时用户在浏览器中禁用cookie李>
请给我一个更好的链接,让我简单地理解它。
谢谢。默认情况下,asp.net使用cookies存储登录用户的会话id。但每个浏览器中都有一个禁用cookie的选项。如果Cookie被禁用,则任何网站都将无法在用户的机器上创建Cookie 禁用Cookie后,会话Id将附加到URL中并传递给服务器。服务器对其进行解码并完成请求的页面
有关详细信息,请参阅此项和此项。默认情况下,asp.net使用cookies存储登录用户的会话id。但每个浏览器中都有一个禁用cookie的选项。如果Cookie被禁用,则任何网站都将无法在用户的机器上创建Cookie 禁用Cookie后,会话Id将附加到URL中并传递给服务器。服务器对其进行解码并完成请求的页面
有关详细信息,请参阅此项和此项。默认情况下,asp.net使用cookies存储登录用户的会话id。但每个浏览器中都有一个禁用cookie的选项。如果Cookie被禁用,则任何网站都将无法在用户的机器上创建Cookie 禁用Cookie后,会话Id将附加到URL中并传递给服务器。服务器对其进行解码并完成请求的页面
有关详细信息,请参阅此项和此项。默认情况下,asp.net使用cookies存储登录用户的会话id。但每个浏览器中都有一个禁用cookie的选项。如果Cookie被禁用,则任何网站都将无法在用户的机器上创建Cookie 禁用Cookie后,会话Id将附加到URL中并传递给服务器。服务器对其进行解码并完成请求的页面
有关更多信息,请参阅此项和此项。MVC的设计不支持无Cookie会话。根据msdn “ASP.NET MVC不支持无Cookie会话和身份验证,我们积极反对” 看 因此,您现在可以选择通过在每个安全控制器的url中传递会话id来实现身份验证,下面是您可以做的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Secure",
url: "{session_id}/{controller}/{action}"
defaults: new
{
session_id=string.Empty,
controller = "Home",
action = "Index"
}
);
}
现在,当用户使用用户名/密码登录时,将其重定向到具有会话id的URL,如
//控制器/操作,您可以构建类似“username | expirytime”的sessionid并对其进行加密,以便在解密时获得username并检查会话是否过期。或者您可以使用类似“会话id”、“用户id”和“到期时间”的模式存储在数据库中
现在你还需要一件事,那就是动作过滤器
public class SessionIdFilter: ActionFilterAttribute
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var sessionid = filterContext.RouteData.Values["session_id"];
//Check session id against db , if exists return;
filterContext.HttpContext.User= new CustomPrincipal("username");//read about custom IPrincipal implementation its easy.
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if( !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(loginurl);
}
}
}
我希望这可以让您开始,您可能需要对如何使用{session_id}参数形成URL“ActionLink”进行一些调整
但在这种情况下也可能发生会话劫持。MVC的设计不支持无Cookie会话。根据msdn “ASP.NET MVC不支持无Cookie会话和身份验证,我们积极反对” 看 因此,您现在可以选择通过在每个安全控制器的url中传递会话id来实现身份验证,下面是您可以做的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Secure",
url: "{session_id}/{controller}/{action}"
defaults: new
{
session_id=string.Empty,
controller = "Home",
action = "Index"
}
);
}
现在,当用户使用用户名/密码登录时,将其重定向到具有会话id的URL,如
//控制器/操作,您可以构建类似“username | expirytime”的sessionid并对其进行加密,以便在解密时获得username并检查会话是否过期。或者您可以使用类似“会话id”、“用户id”和“到期时间”的模式存储在数据库中
现在你还需要一件事,那就是动作过滤器
public class SessionIdFilter: ActionFilterAttribute
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var sessionid = filterContext.RouteData.Values["session_id"];
//Check session id against db , if exists return;
filterContext.HttpContext.User= new CustomPrincipal("username");//read about custom IPrincipal implementation its easy.
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if( !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(loginurl);
}
}
}
我希望这可以让您开始,您可能需要对如何使用{session_id}参数形成URL“ActionLink”进行一些调整
但在这种情况下也可能发生会话劫持。MVC的设计不支持无Cookie会话。根据msdn “ASP.NET MVC不支持无Cookie会话和身份验证,我们积极反对” 看 因此,您现在可以选择通过在每个安全控制器的url中传递会话id来实现身份验证,下面是您可以做的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Secure",
url: "{session_id}/{controller}/{action}"
defaults: new
{
session_id=string.Empty,
controller = "Home",
action = "Index"
}
);
}
现在,当用户使用用户名/密码登录时,将其重定向到具有会话id的URL,如
//控制器/操作,您可以构建类似“username | expirytime”的sessionid并对其进行加密,以便在解密时获得username并检查会话是否过期。或者您可以使用类似“会话id”、“用户id”和“到期时间”的模式存储在数据库中
现在你还需要一件事,那就是动作过滤器
public class SessionIdFilter: ActionFilterAttribute
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var sessionid = filterContext.RouteData.Values["session_id"];
//Check session id against db , if exists return;
filterContext.HttpContext.User= new CustomPrincipal("username");//read about custom IPrincipal implementation its easy.
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if( !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(loginurl);
}
}
}
我希望这可以让您开始,您可能需要对如何使用{session_id}参数形成URL“ActionLink”进行一些调整
但在这种情况下也可能发生会话劫持。MVC的设计不支持无Cookie会话。根据msdn “ASP.NET MVC不支持无Cookie会话和身份验证,我们积极反对” 看 因此,您现在可以选择通过在每个安全控制器的url中传递会话id来实现身份验证,下面是您可以做的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Secure",
url: "{session_id}/{controller}/{action}"
defaults: new
{
session_id=string.Empty,
controller = "Home",
action = "Index"
}
);
}
现在,当用户使用用户名/密码登录时,将其重定向到具有会话id的URL,如
//控制器/操作,您可以构建类似“username | expirytime”的sessionid并对其进行加密,以便在解密时获得username并检查会话是否过期。或者您可以使用类似“会话id”、“用户id”和“到期时间”的模式存储在数据库中
现在你还需要一件事,那就是动作过滤器
public class SessionIdFilter: ActionFilterAttribute
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var sessionid = filterContext.RouteData.Values["session_id"];
//Check session id against db , if exists return;
filterContext.HttpContext.User= new CustomPrincipal("username");//read about custom IPrincipal implementation its easy.
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if( !filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult(loginurl);
}
}
}
我希望这能让你开始,你可能需要