我已经测试过查看使用Firebug和浏览器工具创建的cookie,但是当我登录时,我没有创建cookie。 我已经在〜/ Startup.cs中定义了cookie身份验证(我正在使用Identity框架),我已经检查过我用“Hello,@ User.Identity.Name”来识别。
我的消息来源: 我创建了一个登录ActionResult(〜/ Controller / AccountController.cs)和一个登录模型(〜/ Model / LoginModel.cs),当我用“admin @ admin”登录时(〜/ View / Account / Login.cshtml) .com,“密码”,然后控制器应该创建一个cookie。
〜/控制器/ AccountController.cs
using Microsoft.Owin.Security;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Security.Claims;
//using MyProject.Models;
namespace MyProject.Controllers
{
[AllowAnonymous]
public class AccountController : Controller
{
// GET: account
[HttpGet]
public ActionResult Login(string returnUrl)
{
var model = new LoginModel
{
ReturnUrl = returnUrl
};
return View(model);
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (!ModelState.IsValid)
{
return View();
}
// Don't do this in production!
if (model.Email == "admin@admin.com" && model.Password == "password")
{
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, "Ben"),
new Claim(ClaimTypes.Email, "a@b.com"),
new Claim(ClaimTypes.Country, "England")
},
"ApplicationCookie");
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
// CREATE THE COOKIE
authManager.SignIn(identity);
// Finally we redirect the user agent to the resource they attempted to access. We also check to ensure the return URL is local to the application to prevent Open Redirection attacks
return Redirect(GetRedirectUrl(model.ReturnUrl));
}
// user authN failed
ModelState.AddModelError("", "Invalid email or password");
return View();
}
private string GetRedirectUrl(string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
{
return Url.Action("Index", "Home");
}
return returnUrl;
}
}
}
〜/型号/ LoginModel
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public class LoginModel
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[HiddenInput]
public string ReturnUrl { get; set; }
}
〜/ Startup.cs
using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
//[assembly: OwinStartup(typeof(MyProject.Startup))]
namespace MyProject
{
/// <summary>
/// To initialize the OWIN identity components we need to add a Startup class to the project
/// </summary>
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
// This is a string value that identifies the the cookie. This is necessary since we may have several instances of the Cookie middleware. For example, when using external auth servers (OAuth/OpenID) the same cookie middleware is used to pass claims from the external provider
AuthenticationType = "ApplicationCookie",
// The path to which the user agent (browser) should be redirected to when your application returns an unauthorized (401) response. This should correspond to your "login" controller
LoginPath = new PathString("/Account/Login")
});
}
}
}
〜/视图/ Login.cshtml
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
@Html.EditorForModel()
<p>
<button type="submit">Log In</button>
</p>
}