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;
            //}
        }

    }
}

IXNet会员服务

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

如果您在 ValidateUser 中收到 NullReferenceException,我猜想 _membershipProviderService 为空。你能验证吗?如果是这样,我猜问题是默认构造函数没有实例化_membershipProviderService。

你需要这样的东西:

public XNetMembership() : 
   this(new XNetMembershipProvider())
{

}
于 2013-06-25T02:28:08.497 回答
0

我的猜测是这条线失败了:

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

在这里放一个断点,看看 _membershipProviderService 的值是多少。它是空的吗?那是你的问题。

从您的评论来看,您的问题似乎与 Ninject 相关。

Ninject 不应仅限于您的控制器。如果您有一个依赖于存储库的服务,为什么不在那里也使用 Ninject 呢?它的工作原理与控制器完全相同。然后你就可以轻松测试了。

要解决此问题,您需要与您的成员资格提供商做同样的事情来连接依赖关系,就像您连接控制器一样。我对 Ninject 不熟悉,但也许您在某些配置文件中缺少绑定或属性?

就像是:

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