MVCに組み込まれたforms authenticationを使用できます。 web.configにパスワードを格納できるため、DBは必要ありません。
web.configファイル:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" defaultUrl="~/" protection="None">
<credentials passwordFormat="Clear">
<user name="admin" password="adminpassword" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
新しいMVCプロジェクト(新しいプロジェクト] - > [ASP.NET Webアプリケーションを - > MVC)を作成する場合AccountController
はスキャフォールドしなければなりません。 このコントローラのログインアクション:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl) {
if (!ModelState.IsValid) {
return View(model);
}
// we use simple forms authentication with a list of user in the web.config file
if (FormsAuthentication.Authenticate(model.UserName, model.Password)) {
FormsAuthentication.RedirectFromLoginPage(model.UserName, false);
}
ModelState.AddModelError("", "Wrong username or password");
return View(model);
}