授权属性不保留发布数据

发布于 2024-07-23 03:06:59 字数 480 浏览 5 评论 0原文

为了在控制器方法上使用 [Authorize] 属性后保留发布数据,然后将用户重定向到登录页面,然后在成功验证后将用户重定向到他们打算去的地方 - 这是如何完成的? 默认情况下,不会转发原始表单提交。 对之前帖子的回复说:

您需要将表单值和 RedirectUrl 序列化到隐藏字段。 身份验证后,反序列化隐藏字段中的数据并根据 RedirectUrl 的值进行重定向。 您将需要一个自定义的 Authorize 类来处理这个问题。

我的问题是——有什么例子可以进一步指出我正确的方向吗? 是的,我可以向控制器类添加 [Serialize] 标签,但我不知道创建自定义授权类有何帮助? 我在网上看到了大量有关创建自定义授权类的材料,但是反序列化在哪里完成? 如果你能再深入一两层,将会有很大帮助。 我是新手。

(我会对之前的帖子发表评论,但我是该网站的新手,还没有积累足够的积分。我还会添加另一个帖子的链接,但它说新用户也无法显示链接!)

In order to preserve post data after utilizing the [Authorize] attribute on a controller method, which then redirects the user to a login page, which then upon successful authentication redirects the user to where they were intending to go -- how would this be done? The original form submission is not relayed by default. A response to a previous posting said to:

You need to serialize your form values and a RedirectUrl to a hidden field.
After authentication deserialize the data in your hidden field and redirect based on the value of the RedirectUrl.
You will need a custom Authorize class to handle this.

My question is -- any examples to further point me in the right direction? Yes, I can add a [Serialize] tag to the controller class but I can't figure out how creating a custom Authorize class would help? I see plenty of material online on creating a custom Authorize class but where would the de-serialization be done? It would help greatly if you could go one or two levels deeper. I'm a newbie.

(I would comment on the previous posting but I'm new to the site and have not amassed enough points. I would also put a link to the other posting but it says new users can't show links either!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-07-30 03:06:59

您可以创建一个自定义授权属性,将表单发布的值存储在会话字典中,然后在授权完成后,您可以从会话字典中恢复这些值。
这是一个示例:

public class CustomAuth:AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var form = filterContext.HttpContext.Request.Form;
      if (form.HasKeys()) {
       foreach(var key in form.AllKeys)
       {
         filterContext.HttpContext.Session[key]= form[key];
       }
      }
      base.OnAuthorization(filterContext);
    }
}

如您所见,在授权之前,所有表单值都存储在会话中。
现在,授权完成后,您可以恢复所有值。

[CustomAuth()]
public ActionResult Admin()
{
   // supposing you had a from input with the name "Name"
   string Name = Session["Name"] ?? string.Empty;

   return View();
}

You can create a custom authorization attribue that store the form posted values in the Session dictionary, and then after the authorization has completed you can resotre the values from the Session dictionary.
Here is an example:

public class CustomAuth:AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var form = filterContext.HttpContext.Request.Form;
      if (form.HasKeys()) {
       foreach(var key in form.AllKeys)
       {
         filterContext.HttpContext.Session[key]= form[key];
       }
      }
      base.OnAuthorization(filterContext);
    }
}

As you see, before the authorization all the form values are stored in the session.
Now after the authorization has completed you can restore all the values.

[CustomAuth()]
public ActionResult Admin()
{
   // supposing you had a from input with the name "Name"
   string Name = Session["Name"] ?? string.Empty;

   return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文