ASP.NET MVC: How to Handle Cross TempData and ViewData Actions

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

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

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

Step 2: I use a token to capture user profile information and load a view that allows them to enter any missing required fields and optional fields.

In step 2, I use 2 methods of action: one for processing information capture using a token. The second action, which accepts authorization information and loads a representation of the missing / additional fields.

I pass the authorization information through TempData to the second step. The second action can handle validation, so there is a chance that I will need to hold the authorization object for more than one request. I can’t use the token to regenerate the authorization information, because it is technically a one-time token, and it would be foolish to regenerate the request because it uses network resources.

How can I save objects in my TempData for any subsequent requests to the same action, but delete 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 present a filter attribute that combines TempData (if any) in ViewData. But how will I save my data in future calls of the same action? Throw it in TempData again? And if I detect redirection with empty TempData?

thanks

+4
source share
2 answers

As a result, I again added data to TempData. Since TempData does not allow duplicate keys to be added, I created my own helper method for deleting, and then re-added the key:

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

I had the same problem, but I approached it a little differently - I used a forms authentication method that works exclusively 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 do not need to pass any authentication token - after executing this code, I know that my user is authenticated.

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

After that, validation is performed - I don’t care what comes back with OpenID (i.e. whether it is valid or not), I allow the user to edit this and then save and then perform my check.

+1
source

Source: https://habr.com/ru/post/1285651/


All Articles