ASP.NET MVC:如何处理跨操作 TempData 和 ViewData

发布于 2024-07-23 05:52:06 字数 642 浏览 3 评论 0原文

我正在尝试找到一种好方法来处理以下场景(我对此仍然有点陌生):

用户可以使用 RPX/OpenId 提供商通过我的网站注册。

步骤1:用户通过提供商进行身份验证。 提供程序将临时令牌返回给我的操作方法之一。

第 2 步:我使用令牌来获取用户的个人资料信息并加载一个视图,该视图允许他们输入任何缺少的必填字段和可选字段。

我在步骤 2 中使用了 2 个操作方法:一种是使用令牌来处理信息的抓取。 第二个操作获取授权信息并加载缺少/可选字段视图。

我通过 TempData 将授权信息传递给第二个操作。 第二个操作可以处理验证,因此我有可能需要保留授权对象以处理不止 1 个请求。 我无法使用令牌来重新生成授权信息,因为它在技术上是一次性令牌,并且重新生成请求是愚蠢的,因为它正在使用网络资源。

如何为同一操作的任何后续请求保留 TempData 中的对象,但删除任何重定向的对象? 由于这可能是我的应用程序中的可重复模式,我是否应该创建一个过滤器来自动处理这种情况?

例如,我想象一个过滤器属性会将 TempData(如果有)合并到 ViewData 中 - 但是我如何将我的数据保留到将来对同一操作的调用中? 又扔到TempData里? 如果我检测到重定向清空 TempData?

谢谢

I'm trying to find a good way to handle the following scenario (I'm still kinda new to this):

A user can register through my site using an RPX/OpenId provider.

Step 1: The user authenticates through a provider. The provider returns a temporary token to one of my action methods.

Step 2: I use the token to grab the user's profile information and load a view which allows them to enter any missing required fields and optional fields.

I use 2 action methods in Step 2: One to handle the grabbing of information using the token. A second action which takes the authorization information and loads the missing/optional fields view.

I am passing the authorization info via TempData to the second action. The second action can handle validation so there's a chance I will need to hold on to the authorization object for more than just 1 request. I can't use the token to regenerate the authorization info because it's technically a one-use token, and it would be silly to regenerate the request since it is using network resources.

How could I persist the objects in my TempData for any subsequent requests to the same action, but remove the objects for any redirects? And since this may be a repeatable pattern in my application should I create a Filter to automatically handle this situation?

For example, I imagine a filter attribute which will merge TempData (if any) into ViewData - But how would I persist my data into future calls to the same action? Throw it into TempData again? And if I detect a redirect empty the TempData?

Thanks

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

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

发布评论

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

评论(2

无人问我粥可暖 2024-07-30 05:52:06

如果有必要,我最终将数据重新添加到 TempData 中。 由于 TempData 不允许您添加重复的键,因此我创建了自己的辅助方法来删除然后重新添加键:

public static void AddNew(this TempDataDictionary tempData, string key, object obj)
{
    if ( tempData.ContainsKey( key ) ) tempData.Remove( key );

    tempData.Add( key, obj );
}

I ended up re-adding the data to TempData if necessary. Since TempData does not allow you to add duplicate keys I created my own helper method to remove and then re-add the key:

public static void AddNew(this TempDataDictionary tempData, string key, object obj)
{
    if ( tempData.ContainsKey( key ) ) tempData.Remove( key );

    tempData.Add( key, obj );
}
故人如初 2024-07-30 05:52:06

我遇到了同样的问题,但处理方法略有不同 - 我使用了仅适用于 OpenID 的表单身份验证方法...如果我的提供商返回经过身份验证的,我会执行以下操作

                var fields = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;

                if (fields != null)
                {
                    TempData["Email"] = fields.Email;
                    TempData["Nickname"] = fields.Nickname;
                }

                FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, false);

                break;

这意味着我不需要传递任何类型的身份验证令牌 - 执行此代码后我知道我的用户已通过身份验证。

因此,将控制传递到的操作将简单地将 TempData 字段(如果已填充)复制到 ViewData 中并将它们传递到视图上。

验证在此之后处理 - 我不关心从 OpenID 返回什么(即它是否有效),我让用户编辑它,然后保存,然后执行我的验证。

I've had the same problem, but approached it a slightly different way - I've used a Forms Authentication method which works solely with OpenID... if my provider returns Authenticated, I do the following

                var fields = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;

                if (fields != null)
                {
                    TempData["Email"] = fields.Email;
                    TempData["Nickname"] = fields.Nickname;
                }

                FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, false);

                break;

This means that I don't need to pass any kind of Authentication token around - after this code executes I know my user is authenticated.

Therefore, the action that this passes control to will simply copy the TempData fields, if populated, into ViewData and pass them onto the view.

Validation is handled after this - I don't care what comes back from OpenID (i.e. is it valid or not), I let the user edit this and then save, and then perform my validation.

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