Windows ロール プロバイダーを使用するプロジェクトに取り組んでおり、機能を特定の AD グループに制限したいと考えています。
MVC を使用すると、AuthorizeAttribute
上記のアクション メソッドを使用して、それに応じてリダイレクトできます。MVC を使用しない標準の Web フォーム アプリケーション (.NET 3.5) に対して同様のことができますか?
Windows ロール プロバイダーを使用するプロジェクトに取り組んでおり、機能を特定の AD グループに制限したいと考えています。
MVC を使用すると、AuthorizeAttribute
上記のアクション メソッドを使用して、それに応じてリダイレクトできます。MVC を使用しない標準の Web フォーム アプリケーション (.NET 3.5) に対して同様のことができますか?
これは、web.config で認証要素を使用して設定できます。
<configuration>
<system.web>
<authorization>
<allow roles="domainname\Managers" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
を使用する場合、基本的にドメイン グループはロールに変換されます<authentication mode="Windows" />
。詳細については、MSDN を参照してください。
これは古い投稿であることは知っていますが、これを経験したばかりなので、私の経験を共有したいと思いました. 私は web.config を使いたくありませんでした。MVC の実装に似た Web フォームの属性を作成する方法を探していました。属性部分の基礎として使用したDeran Schillingの投稿を見つけました。
カスタム IPrincipal を作成しました
interface IMyPrincipal : IPrincipal
{
string MyId { get; }
string OrgCode { get; }
string Email { get; }
}
校長
public class MyPrincipal : IMyPrincipal
{
IIdentity identity;
private List<string> roles;
private string email;
private string myId;
private string orgCode;
public MyPrincipal(IIdentity identity, List<string> roles, string myId, string orgCode, string email)
{
this.identity = identity;
this.roles = roles;
this.myId = myId;
this.orgCode = orgCode;
this.email = email;
}
public IIdentity Identity
{
get { return identity; }
}
public bool IsInRole(string role)
{
return roles.Contains(role);
}
public string Email
{
get { return email; }
}
public string MyId
{
get { return myId; }
}
public string OrgCode
{
get { return orgCode; }
}
}
ページで使用するための属性を作成しました
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class AdminAuthorizationAttribute : Attribute
{
public AdminAuthorizationAttribute()
{
var user = (MyPrincipal)HttpContext.Current.User;
if (user.IsInRole("MyAdmin"))
return;
throw new AccessDeniedException();
}
}
いくつかのカスタム例外を作成しました
public class AccessDeniedException : BaseHttpException
{
public AccessDeniedException() : base((int)HttpStatusCode.Unauthorized, "User not authorized.") { }
}
public class BaseHttpException : HttpException
{
public BaseHttpException(int httpCode, string message) : base(httpCode, message) { }
}
そして今、特定のページで使用するために属性を適用できます
[AdminAuthorization]
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}