MVCフィルタのログイン認証

ログインコントローラ

        公共のActionResultインデックス()
        { 
            // 戻りコンテンツ( "ハローインデックス!"); 
            リターンビュー(); 
        
        } 
        [HTTPGET] 
        公共のActionResultログイン()
        { 
            リターンビュー()。
        } 

        [HttpPost] 
        公共のActionResultログイン(文字列名、文字列のパスワード)
        { 
            場合(ユーザ名== " 111 " &&パスワード== " 111 " 
            { 
                セッション[ " UUU " ] =111 返す RedirectToAction(" インデックス" ); 
            } 
            
            { 
                リターンのコンテンツ(" 登录失败!" ); 
            } 
        
        }

インデックス表示

<H2>インデックス</ H2> 
<テキスト>ホームランに成功</テキスト>

ログインビュー

<フォームアクション= " /ログイン/ログイン"メソッド= " ポスト" > 
    用户名の<input type = " テキスト"名前= " ユーザ名"値= "" /> 
    密码の<input type = " テキスト"名前= " パスワード"値= "" /> 
    の<input type = " 提出"名前= " 名前"値= " 提交"/> 

</フォーム>

ログインフィルターをチェック

使用して System.Web.Mvcを。// 别引用错了


 パブリック クラスCheckLoginFilter:IAuthorizationFilter   
    { 


        公共 ボイドOnAuthorization(AuthorizationContext filterContext)
        { 
            ストリングコントローラ名= filterContext.ActionDescriptor.ControllerDescriptor.ControllerName。
            文字列 actionNameの= filterContext.ActionDescriptor.ActionName。
            もし(コントローラ名== " ログイン" && actionNameの== " ログイン" 
            { 
                
            } 
            
            {
                もし(filterContext.HttpContext.Session [ " UUU " ] == NULL 
                { 
                    // ContentResultコンテキスト=新しいContentResult()。
                    // contexts.Content =( "没有登录");
                    // filterContext.Result =コンテキスト。
                    filterContext.HttpContext.Response.Redirect(" /ログイン/ログイン" ); 
                     
                    
                } 
            } 
        } 
    }

グローバル

RouteConfig.RegisterRoutes(RouteTable.Routes)。
 GlobalFilters.Filters.Add(新しいCheckLoginFilter()); //添加フィルター

  

おすすめ

転載: www.cnblogs.com/lierjie/p/11920529.html