2012-06-30 5 views
1

시나리오 I는인가 된 사용자가 새 게시물을 업데이트하고 게시물에 대해 언급 할 수있는 asp.net MVC 3 사용하여 내 관리자 패널에서 포럼을 구축을 위해 노력하고 있어요에 로그인에서 리디렉션 후 POST 값을 보낼 수 있습니다. 아래가 PostsController의 내 댓글 기능입니다는 MVC 3

[Authorize] 
     public ActionResult Comment(int id, string name, string email, string body) 
     { 
      Post post = GetPost(id); 
      Comment comment = new Comment(); 
      User CurrentUser = repository.GetUser(User.Identity.Name); 
      comment.UserID = CurrentUser.ID; 
      comment.Post = post; 
      comment.DateTime = DateTime.Now; 
      comment.Name = User.Identity.Name; 
      comment.Email = email; 
      comment.Body = body; 
      model.AddToComments(comment); 
      model.SaveChanges(); 
      return RedirectToAction("Details", new { id = id }); 

     } 

문제 : 로그온하지 않은 게시물에 누군가 주석, 페이지는 로그인 페이지로 리디렉션 및 사후 경우 로그온 권한이 부여 된 액세스의 포스트 값 주석 형식은 주석 조치로 식별되지 않습니다. 코멘트 양식에 대한 코드는 다음과 같습니다

<form action="@Href("~/Posts/Comment/" + post.ID)" method ="post"> 
      <input type="text" id="commentNamePrompt" name="name" /> 
      Name (required) 
      <br /> 
      <input type="text" id="commentEmailPrompt" name="email" /> 
      Email (optional) 
      <br /> 
      <textarea id="commentBodyInput" name="body" rows="10" cols="60"></textarea> 
      <input type="submit" id="commentSubmitInput" name="submit" value="Submit!" /> 
      </form> 

오류 시도

This property cannot be set to a null value. 

: 나는 내가 있는지 사이트에 먼저 로그인하여 게시물에 댓글을 시도하고 잘 작동 생각했다 로그인 페이지로 리디렉션 한 후 전달되지 않은 모델 게시 값으로 인해 오류가 발생했습니다. 승인되지 않은 사용자가 업데이트 한 의견 양식의 값으로 승인 된 페이지로 리디렉션 할 수 있습니까?

+0

AFAIK, MVC에서 이러한 기능을 지원하는 기본 제공 기능이 없기 때문에이를 위해 "Action Filters"를 확장하거나 다른 방식으로 생각해야합니다. –

답변

1

나는 유일한 방법은 숨겨진 필드로 대체 로그온 양식에 주석 필드를 유지하는 것입니다 생각합니다. 이 작업은 허가되지 않은 요청을 가로 채고 귀하의 코멘트 필드로 대체 로그온 페이지로 리디렉션하여 AuthorizeAttribute 수행 할 수 있습니다

public class CommentAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     var form = filterContext.RequestContext.HttpContext.Request.Form; 
     var commentNamePrompt = form.Get("commentNamePrompt"); 
     var commentEmailPrompt = form.Get("commandEmailPrompt"); 
     var commentBodyInput = form.Get("commentBodyInput"); 
     var url = new UrlHelper(filterContext.RequestContext); 
     var logonUrl = url.Action("LogonWithComment", "Account", new { commentNamePrompt, commentEmailPrompt, commentBodyInput }); 
     filterContext.Result = new RedirectResult(logonUrl); 
    } 
} 

주석 필드는 LogonWithComment보기로 채워 로그온 POST의 일부가 될 수 있습니다.

멋진 해결책은 아닙니다. 가능하다면 나는이 상황을 피할 수있는 방법을 찾는다.

+0

예, 이것은 하나의 대안이지만 나는 적절하지 않을 수도 있다고 생각되는 코멘트를 위해 별도의 로그인 페이지를 만들 필요가 있습니다. 어쨌든, 대체 솔루션을 주셔서 감사합니다. – CodeManiac