0

asp.net でユーザー認証を制御したい。

と仮定する; StackOverflow.aspx と Default.aspx のような 2 つの Web があります。

以下のコードで認証されたロールを取得したい:

public List<Roles> GetAuthenticatedRolesByModuleName(string moduleName)
{
    //below I wrote psedeu code
    var roles = "Select * From SiteRoles Where ModuleName = "Admin";
    //...   
}
//This function returns a result set which includes roles authentication for `moduleName` parameter.

そして、システムにログオンしている現在のロールで制御します。

asp.netのベースページでこれをしたい。

それを行うために、BasePage.csから継承するを作成しSystem.Web.UI.Pageます。GetAuthenticatedRolesByModuleName関数をに書き込みたいのですがBasePage.cs、ユーザーが入力したときにStackOverflow.aspxから関数を呼び出したいのですBasePage.cs

StackOverflow.aspxpageload イベントがあり、での役割を制御する必要があると思いますInit()

ASP.net「BasePage」クラスのアイデアなど、いくつかのソースをグーグルで見つけまし たが、はっきりとはわかりませんでした。

GetAuthenticatedRolesByModuleName(stack-overflow)基本ページ関数 (moduleName は stack-overflow --> ) からロールを取得し、現在のロールで制御したいと考えています 。ユーザーが認証されていない場合は、Default.aspx.

Response.Redirect("Default.aspx");

どうすればいいですか?実装方法を教えてください。

4

1 に答える 1

1

OnpreInitまたはを使用してグローバルにチェックするベース ページを作成すると、次のOnInitようになります。

public abstract class BasePage : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        string cTheFile = HttpContext.Current.Request.Path;

        // here select what part of that string you won to check out
        if(!GetAuthenticatedRolesByModuleName(cTheFile))
        {
            // some how avoid the crash if call the same page again
            if(!cTheFile.EndsWith("Default.aspx"))
            {       
                Response.Redirect("Default.aspx", true);
                return ;
            }
        }

        // continue with the rest of the page
        base.OnPreInit(e);
    }
}

asをglobal.asax使用して同じチェックを行うこともできます。Application_AuthenticateRequest

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    // αυτό είναι το path...
    string cTheFile = HttpContext.Current.Request.Path;

    // here select what part of that string you won to check out
    if(!GetAuthenticatedRolesByModuleName(cTheFile))
    {
        // some how avoid the crash if call the same page again
        if(!cTheFile.EndsWith("Default.aspx"))
        {       
            Response.Redirect("Default.aspx", true);
            Response.End();

            return ;
        }
    }
}       

コードに応じて詳細を追加する必要があるかもしれませんが、これは一般的な考え方です。

于 2012-12-02T22:40:38.610 に答える