且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)

更新时间:2022-09-13 12:16:25

编号:ylbtechASPnetMvcSecurity100010010

1,功能描述

   ASP.net MVC下利用 System.Web.Security.FormsAuthentication类,验证用户的状态(匿名|已登录 )

以项目为例:在视图和和区域里的视图,分别都列举俩个页面(允许匿名和不允许匿名)。

2,技术与环 境

  ASP.net MVC下System.Web.Security.FormsAuthentication类,验证用户的状 态(匿名|已登录)

3,数据库设 计

 无

4,功能截图

 4.1,匿名状态下()

  4.1.1  /Home/Index  网站首页

ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)

 

  4.1.2  /Account/Login  登录

ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)

 

  4.1.3  只要是匿名用户,单击加“[NM]”修饰的地址,都会跳转到/Accout/Login页面

ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)

 

 4.2,已登录状态下

  4.2.1  /Accout/Index  用户中心

ASP.net MVC 中Security.FormsAuthentication验证用户的状态(匿名|已登录)

 

5,代码分析

 5.1,  /web.config  设置重定向登录页面

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

5.2,  /Controllers/AccountController.cs 账户管理控制器    ylb_tip:加“[Authorize]”修饰的方法拒绝匿名

using System.Web.Mvc;
 
using System.Web.Security;
namespace MvcSecurity.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }
         
        //
        //  GET: /Account/Login
 
        [HttpGet]
        public ActionResult Login()
        {
            // 如果是登录状态,则条转到个人主页
            if (Session["Username"] != null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }
 
        //
        // Post: /Account/Login
 
        [HttpPost]
        public ActionResult Login(string username,string userpass)
        {
 
            if (username == "sunshine" && userpass == "m123")
            {
                 
                //创建身份验证票证,即转换为“已登录状态”
                FormsAuthentication.SetAuthCookie(username, false);
                //存入Session
                Session["Username"] = username;
 
                //如果是跳转过来的,则返回上一页面
                if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
                {
                    string returnUrl = Request["ReturnUrl"];
                    return Redirect(returnUrl);
                }
                else
                {
                    //用户个人主页
                    return RedirectToAction("Index");
                }
            }
            else
            {
                ViewData["Tip"] = "用户名或密码有误!";
                return View();
            }
        }
 
        //
        // GET: /Account/Logout
 
        [HttpGet]
        public ActionResult Logout()
        {
            //取消Session会话
            Session.Abandon();
 
            //删除Forms验证票证
            FormsAuthentication.SignOut();
 
            return RedirectToAction("Index", "Home");
        }
    }
}

 

5.3  /Controllers/HomeController.cs  首页控制器(注:区域里面的权限原理相同,在这儿就不多介绍)

using System.Web.Mvc;
 
namespace MvcSecurity.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
 
        public ActionResult Index()
        {
            return View();
        }
 
        //
        // GET: /Home/VipIndex
        [Authorize]
        public ActionResult VipIndex()
        {
            return View();
        }
    }
}
 
6,示例 |讲解案例下载

博客园讲解: http://ylbtech.cnblogs.com/

百度文库开发文档: http://passport.baidu.com/? business&aid=6&un=ylbtech#7

谷歌开源代码下载: http://code.google.com/p/ylbtechaspnetmvc/downloads/list

请单击 “ylbtechASPnetMvcSecurity100010010”

百度网盘  http://pan.baidu.com/s/1i49zn73

请单击 “ASPnetMvcSecurity100010010”

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/08/23/2652394.html,如需转载请自行联系原作者