您的位置:首页 > 编程语言 > ASP

AspNet MVC4 教学-11:Asp.Net MVC4 默认Authorize及自定义Authorize快速Demo

2015-05-07 10:27 423 查看
建立Basic类型的Project

1.HomeController.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAuthorizeTest.Models;
using System.Web.Security;

namespace MvcAuthorizeTest.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(LoginModel login)
        {
            if (login.UserName == "admin" && login.Password == "123456")
            {
                FormsAuthentication.SetAuthCookie(login.UserName, false);
                return Redirect("/Home/ChangePassword");
            }

            return View();
        }

        public ActionResult ChangePassword()
        {
           return View();
        }

        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect("/Home/Login");
        }

        [Authorize]
        public ActionResult ShowDetails()
        {
            return View();
        }

        [Authorize]
        [MyAuthorizeAttribute]
        public ActionResult ShowDetails2()
        {
            return View();
        }

        public ActionResult ShowTip()
        {
            return View();
        }
    }
}

2.Models目录下的文件:

LoginModel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcAuthorizeTest.Models
{
    public class LoginModel
    {
        public string UserName { set; get; }
        public string Password  { set; get; }
    }
}


MyAuthorizeAttribute.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAuthorizeTest.Models
{
    public class MyAuthorizeAttribute : AuthorizeAttribute
    {

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //return base.AuthorizeCore(httpContext);
            return DateTime.Now.Minute % 2 == 0;
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Response.Redirect("/Home/ShowTip");

            //base.HandleUnauthorizedRequest(filterContext);
        }
    }

}
3.Views/Home目录下的View文件如下:

Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@Html.ActionLink("Login","Login")
@Html.ActionLink("Logout","Logout")
<hr />
@Html.ActionLink("ShowDetails", "ShowDetails")
@Html.ActionLink("ShowDetails2", "ShowDetails2")


Login.cshtml:

@model MvcAuthorizeTest.Models.LoginModel

@{
    ViewBag.Title = "Login";
}

<h2>Login</h2>

@using (Html.BeginForm("Login","Home",FormMethod.Post)) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>LoginModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }

    @Html.ActionLink("返回主页","Index")
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
ChangPassword.cshtml:

@{
    ViewBag.Title = "ChangePassword";
}

<h2>ChangePassword</h2>
.....................
@Html.ActionLink("返回主页","Index")
ShowDetails.cshtml:

@{
    ViewBag.Title = "ShowDetails";
}

<h2>ShowDetails</h2>
@Html.ActionLink("返回主页","Index")


ShowDetails2.cshtml:

@{
    ViewBag.Title = "ShowDetails2";
}

<h2>ShowDetails2</h2>
@Html.ActionLink("返回主页","Index")


ShowTip.cshtml:

@{
    ViewBag.Title = "ShowTip";
}

<h2>ShowTip</h2>
时间分值未到偶数.

@Html.ActionLink("返回主页","Index")


4.Web.config修改一处:

<authentication mode="Forms">
      <forms loginUrl="~/Home/Login"</strong> timeout="2880" />
    </authentication>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐