2016-05-12 6 views
0

エンティティフレームワークを使用する既存のデータベースがあります。登録とログインは機能しますが、Signin Status:UnAuthorizedに追加のステータスを追加したいと思います。MVC(ASP.NET ID)カスタムサインインステータスとエンティティフレームワーク

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(model); 
      } 

      // This doesn't count login failures towards account lockout 
      // To enable password failures to trigger account lockout, change to shouldLockout: true 
      var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false); 
      switch (result) 
      { 
       case SignInStatus.Success: 
        return RedirectToLocal(returnUrl); 
       'case SignInStatus.UnAuthorized: 
        return View("UnAuthorized");' //I want to add this 
       case SignInStatus.LockedOut: 
        return View("Lockout"); 
       case SignInStatus.RequiresVerification: 
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
       case SignInStatus.Failure: 
       default: 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
      } 
     } 

私は上記の私が欲しいものをお見せするためにコードスニペットでパーツを追加しました:

は、以下にログインするためのコードです。私は上記のステータスを見つけました。以下のコードスニペットで提供が、私はこのファイルを変更することはできませんよ。

namespace Microsoft.AspNet.Identity.Owin 
{ 
    // 
    // Summary: 
    //  Possible results from a sign in attempt 
    public enum SignInStatus 
    { 
     // 
     // Summary: 
     //  Sign in was successful 
     Success = 0, 
     // 
     // Summary: 
     //  User is locked out 
     LockedOut = 1, 
     // 
     // Summary: 
     //  Sign in requires addition verification (i.e. two factor) 
     RequiresVerification = 2, 
     // 
     // Summary: 
     //  Sign in failed 
     Failure = 3 
    } 
} 

私はMVCに新しいだと追加される場合があります。

カスタムサインインステータスを追加するには、誰かが正しい方向に向いていますか? PS:認可されたフィールドは私のDBに存在します。私のDBには、ユーザが認可されているかどうかを知りたいときに拾いたいところです。

ありがとうございました。

答えて

0

私はグーグルグーグルで、SignInManagerのステータスは変更できないようですね。私はMVCとEntity Frameworkの知識が非常に限られているので、おそらく私は間違っています。

は、だから私は、周りの仕事を見つけて、実際のログインコードが実行される前に、以下を追加することにより、きゅうを使用して独自の検証を行った:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     using (EFNameEntities db = new EFNameEntities()) 
     { 
      var UserData = from ANU in db.AspNetUsers 
          where ANU.UserName == model.UserName 
          select new 
          { 
           ANU.isAuthorized, 
           ANU.isActive 
          }; 

      foreach (var c in UserData) 
      { 
       //If User is NOT Authorized to log in 
       if (!Convert.ToBoolean(c.isAuthorized)) 
       { 
        ModelState.AddModelError("", "This User is not Authorized to Login."); 
        return View(model); 
       } 
       else 
       { 
        //If User is NOT Active 
        if (!Convert.ToBoolean(c.isActive)) 
        { 
         ModelState.AddModelError("", "This User is not Active."); 
         return View(model); 
        } 
       } 
      } 


     } 

     // This doesn't count login failures towards account lockout 
     // To enable password failures to trigger account lockout, change to shouldLockout: true 
     var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       return RedirectToLocal(returnUrl); 
      //case SignInStatus.UnAuthorized: 
      // return View("UnAuthorized"); 
      case SignInStatus.LockedOut: 
       return View("Lockout"); 
      case SignInStatus.RequiresVerification: 
       return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
      case SignInStatus.Failure: 
      default: 
       //Incorrect password or user does not exist 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
     } 
    } 

using (EFNameEntities db = new EFNameEntities()) 
     { 
      var UserData = from ANU in db.AspNetUsers 
          where ANU.UserName == model.UserName 
          select new 
          { 
           ANU.isAuthorized, 
           ANU.isActive 
          }; 

      foreach (var c in UserData) 
      { 
       //If User is NOT Authorized to log in 
       if (!Convert.ToBoolean(c.isAuthorized)) 
       { 
        ModelState.AddModelError("", "This User is not Authorized to Login."); 
        return View(model); 
       } 
       else 
       { 
        //If User is NOT Active 
        if (!Convert.ToBoolean(c.isActive)) 
        { 
         ModelState.AddModelError("", "This User is not Active."); 
         return View(model); 
        } 
       } 
      } 


     } 

だから全体のログインのActionResultはこのようになります

関連する問題