0

メンバーシップ クラスからリポジトリ クラスに値を渡そうとするときに、メンバーシップ プロバイダーとリポジトリ クラスを使用してログイン システムを作成しようとすると、値を渡すことができます。

私のコードの何が問題なのですか??

namespace XNet.Services
{
    public class MembershipProviderService:IMembershipProviderService
    {
        IEntityRepository<User> _UserRepository;

        public MembershipProviderService(IEntityRepository<User> _UserRepository)
        {
            this._UserRepository = _UserRepository;
        }
        public MembershipProviderService()
        {

        }
        public User GetDataLogin(string UserName, string Password)
        {
            //List<User> GetData = _UserRepository.AsQueryable().Where(x => x.UserName == UserName && x.UserPassword == Password).ToList();
            var x = (from d in _UserRepository.All()
                     where d.UserName == UserName && d.UserPassword == Password
                     select d).FirstOrDefault();
            return x;
        }

    }
}

Xnetメンバーシップ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using XNet.Interfaces;
using XNet.Repository.Interfaces;
using XNet.Repository.Model;
using XNet.Services;
using Ninject;

namespace XNet.Security
{
    public class XNetMembership : MembershipProvider
    {
        [Inject]
        public IMembershipProviderService _membershipProviderService { get; set; }
        //private readonly IMembershipProviderService _membershipProviderService;

        //public XNetMembership()
        //{

        //}

        //public XNetMembership(IMembershipProviderService membershipProviderService)
        //{
        //    _membershipProviderService = membershipProviderService;
        //}



        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {

            var x = _membershipProviderService.GetDataLogin(username, password);
            return true;
            //if (x.Count() > 0)
            //{
            //    FormsAuthentication.SetAuthCookie(username, true);
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }

    }
}

IXNetMembershipService

namespace XNet.Interfaces
{
    public interface IMembershipProviderService
    {
        User GetDataLogin(string UserName, string Password);
    }
}

私のコントローラー

public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(User user)
{
    if (Membership.ValidateUser(user.UserName, user.UserPassword))
    {
        return RedirectToAction("Index");
    }

    return View(user);
}

グローバル asx

namespace XNet.WebUI.Hotel
{

    public class NinjectDenpendencyResolver : IDependencyResolver
    {
        private readonly IResolutionRoot _resolutionRoot;

        public NinjectDenpendencyResolver(IResolutionRoot kernel)
        {
            _resolutionRoot = kernel;
        }

        public object GetService(Type serviceType)
        {
            return _resolutionRoot.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _resolutionRoot.GetAll(serviceType);
        }

    }

    internal class MyNinjectModules : NinjectModule
    {
        public override void Load()
        {
            Bind<IMembershipProviderService>()
                .To<MembershipProviderService>();
        }
    }


    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        private IKernel _kernel = new StandardKernel(new MyNinjectModules());

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            _kernel.Inject(Membership.Provider);
        }
    }
}

NinjectWebCommon

[assembly: WebActivator.PreApplicationStartMethod(typeof(XNet.WebUI.Hotel.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(XNet.WebUI.Hotel.App_Start.NinjectWebCommon), "Stop")]

namespace XNet.WebUI.Hotel.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using XNet.Services;
    using XNet.Interfaces;
    using XNet.Repository;
    using XNet.Repository.Interfaces;
    using XNet.Repository.Model;
    using XNet.Repository.Repositories;
    using XNet.Pipelines;
    using System.Web.Security;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {

            EF_RepoRegistration.RegisterRepository(kernel);
            kernel.Bind<IHotelService>().To<HotelService>().InRequestScope();
            //kernel.Bind<IFacilityService>().To<FacilityService>().InRequestScope();
            //kernel.Bind<IHotelFacilityService>().To<HotelFacilityService>().InRequestScope();
            //kernel.Bind<IHotelRoomFacilityService>().To<HotelRoomFacilityService>().InRequestScope();
            kernel.Bind<IRoomTypeService>().To<RoomTypeService>().InRequestScope();
            kernel.Bind<IRoomFacilityService>().To<RoomFacilityService>().InRequestScope();
            kernel.Bind<IRoomTypePipeline>().To<RoomTypePipeline>().InRequestScope();
            kernel.Bind<IAccountService>().To<AccountService>().InRequestScope();
            kernel.Bind<IMembershipProviderService>().To<MembershipProviderService>().InRequestScope();

        }        
    }
}
4

2 に答える 2

0

私の推測では、この行は失敗しています:

var x = _membershipProviderService.GetDataLogin(username, password);

ここにブレーク ポイントを置き、_membershipProviderService の値を確認します。ヌルですか?それはあなたの問題だ。

あなたの問題は、あなたのコメントからNinjectに関連しているようです。

Ninject はコントローラーだけに限定されるべきではありません。リポジトリに依存するサービスがある場合、そこでも Ninject を使用してみませんか? コントローラーとまったく同じように機能します。その後、簡単にテストできます。

この問題を解決するには、メンバーシップ プロバイダーで同じことを行い、コントローラーと同じように依存関係を結び付ける必要があります。私は Ninject に詳しくありませんが、設定ファイルまたは属性でバインディングが欠落している可能性がありますか?

何かのようなもの:

kernel.Bind<IMembershipProviderService >().To<MyAwesomeMembershipProviderService>();
于 2013-06-25T02:40:30.120 に答える