이 정보는 인증 쿠키의 사용자 데이터 부분에 저장됩니다. 그래서 사용자가 로그인 할 때 :
public ActionResult Login(string username, string password)
{
// TODO: validate username/password couple and
// if they are valid get the roles for the user
var roles = "RoleA|RoleC";
var ticket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now,
DateTime.Now.AddMilliseconds(FormsAuthentication.Timeout.TotalMilliseconds),
false,
roles
);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
// IIRC this property is only available in .NET 4.0,
// so you might need a constant here to match the domain property
// in the <forms> tag of the web.config
Domain = FormsAuthentication.CookieDomain,
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
};
Response.AppendCookie(authCookie);
return RedirectToAction("SomeSecureAction");
}
은 그 때 나는 독서를 돌봐 및 인증 티켓을 구문 분석하고있는 HttpContext에서 일반 사용자를 저장하는 사용자 정의 authroize 속성을 작성합니다.이에 대응하는 역할을 가지고있는 사용자 속성 :
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext.User.Identity.IsAuthenticated)
{
var authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
var roles = ticket.UserData.Split('|');
var identity = new GenericIdentity(ticket.Name);
httpContext.User = new GenericPrincipal(identity, roles);
}
}
return base.AuthorizeCore(httpContext);
}
}
다음 당신은 당신의 컨트롤러를 장식 할 수 /이 속성과 행동을 처리 할 수있는 권한 :
사용자가 단순히 주어진 역할에 있는지 여부를 확인하기 위해
// Only users that have RoleA or RoleB can access this action
// Note that this works only with OR => that's how the base
// authorize attribute is implemented. If you need to handle AND
// you will need to completely short-circuit the base method call
// in your custom authroize attribute and simply handle this
// case manually
[MyAuthorize(Roles = "RoleA,RoleB")]
public ActionResult Foo()
{
...
}
:
bool isInRole = User.IsInRole("RoleC");
이 정보로 무장 한 이제보기 모델을 구성하는 방법에 대해 생각할 수 있습니다. 이러한 뷰 모델에는 CanEdit
, CanViewReport
등과 같은 부울 속성이 포함됩니다. 컨트롤러에 의해 채워집니다.
이제 각 동작 및보기에서이 매핑이 필요할 경우 반복적이고 지루할 수 있습니다. 이것은 글로벌 사용자 지정 작업 필터가 작동하는 곳입니다 (ASP.NET MVC 2에만 존재하지는 않지만 ASP.NET MVC 3에만 존재하므로이 동작 필터로 장식 된 기본 컨트롤러가 필요함) 기능). 당신은 단순히 각각의 액션 후에 실행되는 글로벌 액션 필터를 정의하고 ViewData (holy ...., 나는 그 단어들을 발음하고 있다고 믿을 수 없다)에 대한 몇 가지 공통 뷰 모델을 주입하여, 그것들을 transverse의 모든 뷰에서 사용할 수있게 만든다. 다른 행동 방식.
마지막으로보기에서 해당 부울 값 속성을 확인하여 사이트의 다른 영역을 포함하거나 포함하지 않을 것입니다. 지금까지 자바 스크립트 코드가 중요하지 않은 경우 사이트의 영역을 조심스럽게 AJAX 화하면 해당 영역이 DOM에없는 경우이 코드는 실행되지 않습니다. 더 세분화 된 제어가 필요한 경우 항상 HTML5 data-*
속성을 DOM 요소에 사용하여 사용자의 권한에 대한 외부 자바 스크립트 기능에 대한 힌트를 제공 할 수 있습니다.
web.config의 위치 기반 권한 부여 보안 설정으로 제어 할 수 없습니까? – Holystream