認証用DBの作成方法
テーブルを生成する
Code Firstで実行時に生成されるのはデモとしてはいいのですけど、実務では開発者の意図するタイミングで生成しておきたいこともあります。その場合は、Migrationしてやればいいようです。
ツール -> NuGetパッケージマネージャー -> パッケージマネジャーコンソールを開き、
Enable-Migrations
Add-Migration migration_filename
Update-Database
認証用DBの作成方法
Code Firstで実行時に生成されるのはデモとしてはいいのですけど、実務では開発者の意図するタイミングで生成しておきたいこともあります。その場合は、Migrationしてやればいいようです。
ツール -> NuGetパッケージマネージャー -> パッケージマネジャーコンソールを開き、
Enable-Migrations
Add-Migration migration_filename
Update-Database
var AutheticationManager = HttpContext.GetOwinContext().Authentication;
AuthenticationManager.SignOut();
ClaimsIdentityを生成する方法が
分からんので、
単純にセッション変数を使ってログインを使用かな~と思ったがusing Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System.Security.Claims;
using System.Web;
using System.Web.Mvc;
namespace Mvc5AuthSample.Controllers
{
public class AccountController : Controller
{
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string username, string password)
{
if (username == "alice" && password == "supersecret")
{
HttpContext.GetOwinContext().Authentication
.SignOut(DefaultAuthenticationTypes.ExternalCookie);
Claim claim1 = new Claim(ClaimTypes.Name, username);
Claim[] claims = new Claim[] { claim1 };
ClaimsIdentity claimsIdentity =
new ClaimsIdentity(claims,
DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication
.SignIn(new AuthenticationProperties()
{ IsPersistent = false }, claimsIdentity);
return Redirect("/Home");
}
else
{
ModelState.AddModelError("",
"Invalid username or password.");
}
return View();
}
}
}