여기에 동작하는 예제입니다 :
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeByConfig : AuthorizeAttribute
{
/// <summary>
/// Web.config appSetting key to get comma-delimited roles from
/// </summary>
public string RolesAppSettingKey { get; set; }
/// <summary>
/// Web.config appSetting key to get comma-delimited users from
/// </summary>
public string UsersAppSettingKey { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (!String.IsNullOrEmpty(RolesAppSettingKey))
{
string roles = ConfigurationManager.AppSettings[RolesAppSettingKey];
if (!String.IsNullOrEmpty(roles))
{
this.Roles = roles;
}
}
if (!String.IsNullOrEmpty(UsersAppSettingKey))
{
string users = ConfigurationManager.AppSettings[UsersAppSettingKey];
if (!String.IsNullOrEmpty(users))
{
this.Users = users;
}
}
return base.AuthorizeCore(httpContext);
}
}
그래서 같은 컨트롤러 클래스 나 메소드를 장식 :
[AuthorizeByConfig(RolesAppSettingKey = "Authorize.Roles", UsersAppSettingKey = "Authorize.Users")]
하는 Authorize.Roles 및 Authorize.Users가 appSettings는 web.config 반면되는 경우.
이것은 과도한 것처럼 보입니다. MVC에서 사용하는 특정 역할 이름을 만드는 설치 요구 사항을 만드는 것이 나을 것이라고 생각하십니까? – M4V3R1CK
@ M4V3R1CK의 경우 사용자 지정 인증 특성이 중요하지 않습니다. 체크 아웃 http://msdn.microsoft.com/en-us/library/ee707357(v=vs.91).aspx –
MVC가 Windows의 역할을 인식하지 못하는 곳에서 발생하는 사람이 있습니까? 그 역할을 Windows에서 그룹으로 변환 할 수 있다고 생각했는데 어떤 이유로 사용자를 그룹에 추가 할 때 해당 user.IsInRole ("GroupJustAddedTo")이 항상 false를 반환하면 MVC (Windows 인증 사용)를 확인하십시오. 왜 그런지 모르겠다 .... Windows 2003 R2에서 Windows 인증을 사용하여 작업 할 수 있습니다. 혼동 : ~ (??? – M4V3R1CK