これは私の髪を引っ張っています。どんな助けでも大歓迎です。
.NET Framework 4.6.1 で MVC (5 だと思います) アプリケーションを実行しています。アイデンティティ認証を使用しています。このサイトは約 1 年前に古いコンピューターで作成しました。私は過去1年間、オンとオフに取り組んでいましたが、この問題は一度もありませんでした. それから最近、私は新しいラップトップを手に入れ、GitHub からプロジェクトを複製しました。突然、デバッグ中に UserManager がこの「2 番目の操作」エラーを表示し、それを呼び出す場所をステップスルーするという奇妙な問題が発生しました。私のサイトにはたくさんの場所があります。古いマシンでこれを行ったことは一度もないので、設定を変更する必要があるのか 、それとも古いマシンのコードが偶然に欠落しているのかはわかりませんが、そうではないようです.
UserManager をデフォルトの方法でセットアップしました。これにより、OwinContext の一部として作成されます。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<AppUsersDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(AppUsersDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
//{
// ClientId = "",
// ClientSecret = ""
//});
}
}
ご覧のとおり、少なくとも私が知る限り、これはすべてかなり標準的なものです。Visual Studio で作成されて以来、このコードに大きな変更を加えたことはないと思います。
呼び出されたときの概念は、親コントローラーに AppUser プロパティを持たせて、データ ストレージ呼び出しを行うために必要な現在のユーザーに関する情報を取得できるようにすることです。この AppUser を取得するために、UserManager と FindByName を呼び出します。
public ApplicationUser AppUser
{
get
{
if (appUser == null) { appUser = UserManager.FindByName(User.Identity.Name); }
return appUser;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
set
{
_userManager = value;
}
}
次に、以下の例でその AppUser を呼び出します。
[HttpPost]
[HasAccess(Priviledge = AppLogic.Priviledge.DM)]
public JsonResult UpdateActivityLog(ActivityLogPostModel model)
{
try
{
CampaignSvc.UpdateActivityLog(AppUser.UserId, AppUser.ActiveCampaign.Value, model.ArcKey, model.LogKey, model.LogDescription, model.Type, model.ContentKey);
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
return Json(new { success = true, message = "Log added successfully!" });
}
上記の例は、多くの例の 1 つにすぎません。これは、この特定のインスタンスに関するものではありません。ほとんどの場合、サイト内のどこでも選択できます。したがって、この「2 番目の操作」エラーが発生する理由の 1 つは、呼び出しの非同期バージョンを呼び出していないことです。おそらくそうすべきですが、そうではありませんが、それでもこのエラーが発生します。さらに、localhost を使用してデバッグ モードでサイトを実行しても、このエラーは発生しません。サイトで必要な操作はすべて実行でき、問題は発生しません。しかし、ランダムに、デバッグ中にブレークポイントがあり、ステップ実行している場合、操作が失敗し、この「2 番目の操作」例外が発生します。これも、古いコンピューターでは一度も発生しませんでした。インスタンスを停止して再起動すると、すぐにこのエラーが表示されます。IIS Express がタスク バーで実行されていない場合でも、古いインスタンスを引き続き保持します。通常、インスタンスを数回再起動するか、10 分間休憩してから再起動することで、停止させています。しかし、確かに、その呼び出しを行うブレークポイントを介してデバッグすると、エラーが返されます。
私に与えるアイデアや卑劣なキャップはありますか?