実行時にweb.configから値を読み取る独自のAuthorizationAttributeクラスを作成する必要があります。 .NETでは、ランタイム依存の値を使用して属性を宣言することはできません。
[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);
}
}
そしてそうのようなあなたのコントローラクラスやメソッドを飾る:
Authorize.RolesとAuthorize.UsersはのappSettingsをweb.configファイルされている
[AuthorizeByConfig(RolesAppSettingKey = "Authorize.Roles", UsersAppSettingKey = "Authorize.Users")]
ここ
これは過度のようです。 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