내 사용자는 인증되지만 추가 정보가 FormsAuthenticationTicket에 저장된 다음 쿠키가 있습니다.레이아웃 페이지에서 사용자 정의 Userdata의 모델보기
// Custom UserData will contain the following | seperated values:
// [ FullName | ContactNumber1 | ContactNumber2 ]
string userData = fullName + "|" + contactNumber1 + "|" + contactNumber2
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(20),
false,
userData,
FormsAuthentication.FormsCookiePath);
는 쿠키 등 등
이 모든 잘 작동 만듭니다. MVC 3를 사용하여
임과 면도기
나는 페이지 상단의 전체 이름/ContactNumber 등 등을 표시하는 foo.Layout.cshtml 있습니다.
내가 디스플레이에게 이러한 값을 추출하기 위해 foo.Layout.cshtml의 상단에 코드를 포함 할 수있다 :
@{
FormsAuthenticationTicket ticket = ((FormsIdentity)User.Identity).Ticket;
// Custom UserData will contain the following | seperated values:
// [ FullName | ContactNumber1 | ContactNumber2 ]
string[] userData = ticket.UserData.Split(new char[] { '|' });
string fullName = userData[0];
string contactNumber1 = userData[1];
string contactNumber2 = userData[2];
}
<div>@fullName</div>
<div>@contactNumber1</div>
<div>@contactNumber2</div>
이것은 내가 내 컨트롤러에서이 코드를 포함해야하지만 그나마 정직하게 해달라고 의미를 그것과 내 실제 애플 리케이션 (다른 가치)에서 레이아웃을 포함한 내 의견의 대다수에 가치가 필요합니다.
public class UserViewModel {
public string FullName { get; set; }
public string ContactNumber1 { get; set; }
public string ContactNumber2 { get; set; }
}
이 컨트롤러에게 어쩌면이 중 일부는 컨트롤러 공장 내부에 넣을 수
public class FooController : Controller {
private readonly _userViewModel = null;
public FooController() {
// NOTE this would actually be behind an interface in some type of
// repository/service pattern and injected but for the sake
// of easy conversation ive put it here.
FormsAuthenticationTicket ticket = ((FormsIdentity)User.Identity).Ticket;
// Custom UserData will contain the following | seperated values:
// [ FullName | ContactNumber1 | ContactNumber2 ]
string[] userData = ticket.UserData.Split(new char[] { '|' });
_userViewModel = new UserViewModel();
userViewModel.FullName = userData[0];
userViewModel.ContactNumber1 = userData[1];
userViewModel.ContactNumber2 = userData[2];
}
public ActionResult Index() {
(HOWEVER_SET_ON_LAYOUT_PAGE) = userViewModel.FullName;
// If not posting ViewModel
(HOWEVER_SET_ON_THIS_PAGE) = userViewModel.FullName;
// If posting ViewModel
return View(_userViewModel);
}
만들기 :
는 뷰 모델을 만들기 :
그래서 나는 내 컨트롤러 내부의 코드를 포함 논의했다 .다른 사람들이 이러한 일반적인 설정에 어떻게 접근하고 있는지에 대한 토론이나 아이디어를 찾고 있습니다.
내 실제 작업은 레이아웃을 통해 각 페이지의 맨 위에 사용자 성명을 표시하고 제출 된 여러 양식의 맨 아래에 성명 (및 기타 데이터)을 표시하는 것입니다. 다시 게시 후 전체 이름은 쿠키에서 다시 읽혀지고 다른 양식 필드와 함께 저장되도록 전송됩니다. 그게 의미있는 일이며 실제로 그 때입니까? -/
실제 예제를 제공 할 수 있습니까 ?? –