この質問を前もってお詫び申し上げます。私はまだSOに慣れていません。
私は、MVC5、EF6、および VS 2013 を使用して Web アプリケーションに取り組んできました。
リリースされたRCビットへのアップグレードに時間を費やしました。そこにあるすべての素晴らしい投稿に感謝します。Microsoft.AspNet.Identity.*の分離とasp.net MVC の 5.0.0-beta2 から 5.0.0-rc1 への更新!
私の無限の知恵で、@ Hao Kung がここに投稿した RTM ビットに移動することにしました。. 最終的にRTMビルドを受け取ったときに、私はトラブルを回避し、それほど遅れることはないと考えました.
これは悪夢だったか、RC1 で作業していた基本的なタスクを把握できないため、何か (またはその両方) を完全に見逃しているだけです。
コントローラー経由でユーザーをログインしているように見えますが ( Asp.Net Identity RTM バージョンの Microsoft.AspNet.Identity.Owin.AuthenticationManager はどこにありますか? ) ... WindowsIdentity は常に空で、SignIn を呼び出した後は認証されません。ユーザーと 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);
}
(ちなみに、現時点では外部ユーザーにログインする必要はありません。)
助言がありますか?- または - すべての変更をロールバックして、VS 2013 が RTMd になるまで待つ必要がありますか?
@Hao Kung の元の返信に近づけるためにコードを更新し、リファクタリングしました。ただし、有効なユーザー ID を取得することはまだできません。AuthenticationManager が正しく割り当てられていないと思いますか?
AuthenticationManger は次のように定義されるようになりました。
public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
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);
}
「サインアウト」の後、デバッガーは次のように表示します。
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
「claimsIdentity」は次のとおりです。
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"
「サインイン」は何も変更しません:
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
まだ認証はありませんが、エラーはスローされていないようです。
@Hao Kung が回答したように、StartUp.Auth.cs を次のように変更しました。
var authOptions = new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(4.0)};
app.UseCookieAuthentication(authOptions);
に:
var authOptions = new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromHours(4.0)
}; ...