ASP.NetMVCでのアイデンティティ認証
7048 ワード
@using (Html.BeginForm("Login", "Home"))
{
<input type="text" name="adminName" />
<br />
<input type="password" name="pwd" />
<input type="submit" name="name" value=" " />
}
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
string name = HttpContext.Request.Form["adminName"];
string pwd = HttpContext.Request.Form["pwd"];
// , 。
// session 。
Session["user"] = name;
// , Cookie。
System.Web.Security.FormsAuthentication.SetAuthCookie(name, false);
return Content("123");
}
[Authorize]
public ActionResult DoSth()
{
// Authorize , 。
if (this.User.Identity.IsAuthenticated)//
{
//string adminName = this.User.Identity.Name;// adminName。
}
return View();
}
[Authorize]
public ActionResult LoginOut()
{
System.Web.Security.FormsAuthentication.SignOut();// , , this.User.Identity.IsAuthenticated true
Session["user"] = null;// session
return RedirectToAction("Index", "Home");
}
}
}
Web.configファイルで関連構成を行います.有効期限は30分です.ジャンプページ
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Home/Index" timeout="30">forms>
authentication>