ASP.NET MVC - save POST data after login attribute authorization authorization

I have a blog page with comments. Any user (registered or not) can see the form at the bottom of the page to post a comment. When a user enters a comment, and it is not authorized, the user is redirected to the login / registration page. After logging in, the user is redirected back to the action, but the POST data containing the comment body is lost.

I use the Authorize MVC ASP.NET attribute to request authorization for some actions:

[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(int blogPostID, string commentBody) {
    var comment = new Comment {
       Body = commentBody,
       BlogPostID = blogPostID,
       UserName = User.Identity.Name
    }
    // persist the comment and redirect to a blog post page with recently added comment
}

How do you solve this problem?

Creating a user login before displaying the comment form is a bad idea, I think.

Thank.

+3
source
2

, , siteId . Create, . , - , Create.

Authorize . - :

var user = HttpContext.User;

if (!user.Identity.IsAuthenticated)
{ 
   Session["Comment"] = comment;
   Session["SiteId"] = siteId;
   return RedirectToAction("LogOn", "Account", 
                           new { returnUrl = "/ControllerName/Create"} );
}

Create:

public ActionResult Create()
{
    var comment = (Session["Comment"] ?? "").ToString();
    int siteId = 0;
    if (Session["siteId"] != null)
        siteId = (int)Session["siteId"];

    return Create(siteId, comment);
}

, - , . (, , ). , - , .

+3

. ( POST). , . , , .

+1

All Articles