ACG*_*ACG 47 c# owin visual-studio-2013 asp.net-mvc-5 asp.net-identity
道歉并提前感谢您提出这个问题!我还是SO的新手.
我一直在使用MVC5,EF6和VS 2013开发Web应用程序.
一旦发布,我花了一些时间升级到RC位.感谢所有伟大的帖子:例如.解耦Microsoft.AspNet.Identity.*并将asp.net MVC从5.0.0-beta2更新到5.0.0-rc1!
在我无限的智慧中,我决定转向@Hao Kung在这里发布的RTM位:我如何能够及早访问即将到来的Asp.Net Identity变化?.我认为当我们最终收到RTM版本时,我会省去麻烦并且不会太落后.
这可能是一场噩梦,或者我只是完全遗漏了某些东西(或两者兼而有之),因为我无法弄清楚曾经使用RC1的基本任务.
虽然我似乎是通过控制器登录用户(Asp.Net Identity RTM版本中的Microsoft.AspNet.Identity.Owin.AuthenticationManager在哪里?)...我的WindowsIdentity始终为空,在我调用SignIn后未经过身份验证.正确填充了user和claimIdentity对象.
这是我调用的操作方法(为了完整性将属性移动到局部变量):
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public virtual async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid) {
var userManager = new UserManager<EtdsUser>(new UserStore<EtdsUser>(new EtdsContext()));
var user = userManager.Find(model.UserName, model.Password);
if (user != null) {
var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer});
var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity);
return RedirectToLocal(returnUrl);
}
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
(旁注:此时我不需要登录外部用户.)
有什么建议? - 或者 - 我应该回滚所有更改,并等到VS 2013是RTMd吗?
更新,重构代码,使其更接近@Hao Kung的原始回复.但是,我仍然没有得到有效的用户身份.我认为我的AuthenticationManager没有正确分配?
AuthenticationManger现在定义为:
public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
Run Code Online (Sandbox Code Playgroud)
SignInAsync现在是一个单独的方法:
private async Task SignInAsync(EtdsUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var claimsIdentity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, claimsIdentity);
}
Run Code Online (Sandbox Code Playgroud)
在"SignOut"之后,调试器显示:
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
Run Code Online (Sandbox Code Playgroud)
那么"claimIdentity"就是:
claimsIdentity
{System.Security.Claims.ClaimsIdentity}
Actor: null
AuthenticationType: "ApplicationCookie"
BootstrapContext: null
Claims: {System.Security.Claims.ClaimsIdentity.get_Claims}
IsAuthenticated: true
Label: null
Name: "alon"
NameClaimType: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
RoleClaimType: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
Run Code Online (Sandbox Code Playgroud)
"SignIn"不会改变任何东西:
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
Run Code Online (Sandbox Code Playgroud)
仍然没有身份验证,但似乎没有抛出任何错误.
正如@Hao Kung所回答的,改变了StartUp.Auth.cs:
var authOptions = new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(4.0)};
app.UseCookieAuthentication(authOptions);
Run Code Online (Sandbox Code Playgroud)
至:
var authOptions = new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromHours(4.0)
}; ...
Run Code Online (Sandbox Code Playgroud)
Hao*_*ung 41
所以这里的RTM基本上是什么样的(从ASPNET Identity示例代码复制的代码):
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
Run Code Online (Sandbox Code Playgroud)
编辑:您需要在Startup.Auth.cs中进行以下更改:
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80842 次 |
| 最近记录: |