私はWindowsロールプロバイダを使用するプロジェクトに取り組んでおり、特定のADグループに機能を限定したいと考えています。には、。NET用の標準Webフォーム(MVCではなく)に相当するauthorize属性があります。
MVCを使用すると、自分のアクションメソッドの上にAuthorizeAttribute
を使用してリダイレクトできます。 MVCを使用しない標準のWebフォームアプリケーション(.NET 3.5)でできることは何ですか?
私はWindowsロールプロバイダを使用するプロジェクトに取り組んでおり、特定のADグループに機能を限定したいと考えています。には、。NET用の標準Webフォーム(MVCではなく)に相当するauthorize属性があります。
MVCを使用すると、自分のアクションメソッドの上にAuthorizeAttribute
を使用してリダイレクトできます。 MVCを使用しない標準のWebフォームアプリケーション(.NET 3.5)でできることは何ですか?
これは、web.configでauthorization要素を使用して設定できます。 <authentication mode="Windows" />
を使用した場合、基本的にドメイングループが役割に翻訳され
<configuration>
<system.web>
<authorization>
<allow roles="domainname\Managers" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
。 することができますread more about it on MSDN
私はこれが古い投稿ですが、私はちょうどこれを通過したとして私の経験を共有すると思った。私はweb.configを使いたくなかった。 MVCの実装に似たWebformsの属性を作成する方法を探していました。私は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();
}
}
を使用するために属性を作成し、いくつかのカスタム例外
012を作成しました、今私は
[AdminAuthorization]
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
私はこれが非常に良い例ではないと思います。恐れています。属性のコンストラクタにコードがあります。これは、ユーザーの資格情報のコードで依存しているHttpContextが、あなたが考えるコンテキストではない可能性があることを意味します。これは、いくつかのテストケースでは機能しますが、可能性があります – AlexC
@AlexCこのコードの代替手段はありますか?(webforms) –
このコードはWebFormsでは動作しませんが、HttpExceptionsはスローされません。 –
右私はそれを持って、特定のページに使用するために属性を適用することができます。しかし、特定のメソッドレベルでアクセスを制限する方法は、MVCで行う方法と似ていますか? –
@Blair Jones、メソッド内で 'if(User.IsInRole(" somerole "))'のようなチェックをしなければならないでしょう、私は恐れています。 –
okありがとう...それは私が恐れていたものです:( –