سؤال

إنني أتبع نموذجًا من Microsoft لتنفيذ التحقق من صحة البريد الإلكتروني باستخدام Identity 2.0.0

أنا عالقة في هذا الجزء

public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}

هذا يعمل في controller لكن HttpContext لا يحتوي على أي GetOwinContext طريقة في APIController.

لذلك حاولت HttpContext.Current.GetOwinContext() ولكن الطريقة GetUserManager غير موجود.

لا أستطيع معرفة طريقة للحصول على UserManager أنا أبني في Startup.Auth.cs

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    //Configure the db context, user manager and role manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    ...
}

هذا الخط

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

يستدعي الوظيفة التالية لتكوين UserManager

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
     //Configure validation logic for usernames
     manager.UserValidator = new UserValidator<ApplicationUser>(manager)
     {
         AllowOnlyAlphanumericUserNames = false,
         RequireUniqueEmail = true
     };

     // Configure user lockout defaults
     manager.UserLockoutEnabledByDefault = true;
     manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
     manager.MaxFailedAccessAttemptsBeforeLockout = 5;

     manager.EmailService = new EmailService();

     var dataProtectionProvider = options.DataProtectionProvider;
     if (dataProtectionProvider != null)
     {
         manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
     }
     return manager;
}

كيف يمكنني الوصول إلى هذا UserManager في ApiController?

هل كانت مفيدة؟

المحلول

لقد أساءت حقا فهم سؤالك في وقت سابق.أعتقد أنك تفتقد بعض عبارات الاستخدام.

ال GetOwinContext().GetUserManager<ApplicationUserManager>() في داخل Microsoft.AspNet.Identity.Owin.

لذا حاول إضافة هذا الجزء:

using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity; // Maybe this one too

var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();

نصائح أخرى

قد تكون طريقة الامتداد هذه حلاً أفضل إذا كنت تريد اختبار وحدة التحكم الخاصة بك.

using System;
using System.Net.Http;
using System.Web;
using Microsoft.Owin;

public static IOwinContext GetOwinContext(this HttpRequestMessage request)
{
    var context = request.Properties["MS_HttpContext"] as HttpContextWrapper;
    if (context != null)
    {
        return HttpContextBaseExtensions.GetOwinContext(context.Request);
    }
    return null;
}

الاستخدام:

public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}

هذا السطر الوحيد من التعليمات البرمجية أنقذ يومي ...

       var manager = 
       new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));

يمكنك استخدامه ضمن إجراء وحدة التحكم للحصول على مثيل UserManager.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top