MembershipProvider授权

时间:2015-01-15 19:28:06

标签: c# asp.net-mvc authorization asp.net-membership membership-provider

在我的mvc 4网络应用程序中,我有一个用于授权的自定义成员提供者。但我也不允许登录用户的授权。我需要知道原因是什么。我只实现了ValidateUser方法。我是否需要实现任何其他重写方法?请帮忙。

如果您需要任何其他信息,请在下面发表评论,我可以给予。

这是我的membershipProvider类

using ABC.DTS.DAO;
using ABC.DTS.DAO.DB;
using ABC.DTS.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

    namespace ABC.DTS.Providers
    {

        public class DTSMembershipProvider : MembershipProvider
        {
            public override string ApplicationName
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            public override bool ValidateUser(string username, string password)
            {
                try
                {
                    MemberHandler memberHandler = new MemberHandler();
                    LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser, Entry, , EmailAddress = " + username + "Password = " + password);

                    LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser-databaseOpen, Entry, , EmailAddress = " + username);
                    Member user = memberHandler.GetMemberByEmail(username);

                    if (user == null)
                    {

                        LogFileManager.Instance.GetLogger("error").Debug("DTSMembershipProvider, ValidateUser, Error, Invalid Email Address, EmailAddress = " + username + " Password = " + password);
                        return false;
                    }

                    else
                    {
                        if (user.Password == password && user != null)
                        {
                            LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser, OK, Successful Login, EmailAddress = " + username + "Password = " + password);
                            return true;
                        }

                        else
                        {
                            LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser, Error, Password Error, EmailAddress = " + username + " Password = " + password);
                            LogFileManager.Instance.GetLogger("error").Debug("DTSMembershipProvider, ValidateUser, Error, Password Error, EmailAddress = " + username + " Password = " + password);
                            return false;
                        }
                    }

                }

                catch (DAOException de)
                {
                    LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser, Error, " + de.Message + ", EmailAddress = " + username + "Password = " + password);
                    LogFileManager.Instance.GetLogger("error").Debug("DTSMembershipProvider, ValidateUser, Error, " + de.Message + ", EmailAddress = " + username + "Password = " + password);
                    throw de;
                }

                catch (Exception e)
                {
                    LogFileManager.Instance.GetLogger("event").Debug("DTSMembershipProvider, ValidateUser, Error, " + e.Message + ", EmailAddress = " + username + "Password = " + password);
                    LogFileManager.Instance.GetLogger("error").Debug("DTSMembershipProvider, ValidateUser, Error, " + e.Message + ", EmailAddress = " + username + "Password = " + password);
                    throw new DAOException(2002,"Internal Error",e);
                }
            }

            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();
            }
        }
    }

这也是我的控制器类

using ABC.DTS.DAO;
using ABC.DTS.Login;
using ABC.DTS.Models;
using ABC.DTS.Providers;
using ABC.DTS.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ABC.DTS.Controllers
{
    public class AuthenticationController : Controller
    {
        //
        // GET: /Authentication/

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

        public ActionResult Login(string username, string password, int loginMode)
        {
            GenericModel genericmodel;
            try
            {
                LogFileManager.Instance.GetLogger("event").Debug("AuthenticationController, Login, Entry, , EmailAddress = " + username + "Password = " + password);

                ILoginHandler loginHandler;
                LoginHandlerFactory loginHandlerFactory = new LoginHandlerFactory();

                loginHandler = loginHandlerFactory.GetLoginHandler(loginMode);

                if (loginHandler.Login(username, password))
                {
                    LogFileManager.Instance.GetLogger("event").Debug("AuthenticationController, Login, OK, Successful Login, EmailAddress = " + username + "Password = " + password);
                    genericmodel = new GenericModel(0000, "Successfully logged in");
                    return Json(genericmodel);
                }
                else
                {
                    LogFileManager.Instance.GetLogger("event").Debug("AuthenticationController, Login, Error, Password Error, EmailAddress = " + username + " Password = " + password);
                    LogFileManager.Instance.GetLogger("error").Debug("AuthenticationController, Login, Error, Password Error, EmailAddress = " + username + " Password = " + password);
                    genericmodel = new GenericModel(1001, "Invalid username or password");
                    return Json(genericmodel);
                }
            }
            catch (DAOException de)
            {
                LogFileManager.Instance.GetLogger("event").Debug("AuthenticationController, Login, Error, " + de.Message + ", EmailAddress = " + username + "Password = " + password);
                LogFileManager.Instance.GetLogger("error").Debug("AuthenticationController, Login, Error, " + de.Message + ", EmailAddress = " + username + "Password = " + password);
                genericmodel = new GenericModel(de.Code, de.Message);
                return Json(genericmodel);
            }
            catch (Exception e)
            {
                LogFileManager.Instance.GetLogger("event").Debug("AuthenticationController, Login, Error, " + e.Message + ", EmailAddress = " + username + "Password = " + password);
                LogFileManager.Instance.GetLogger("error").Debug("AuthenticationController, Login, Error, " + e.Message + ", EmailAddress = " + username + "Password = " + password);
                genericmodel = new GenericModel(2002, "Internal Error");
                return Json(genericmodel);
            }
        }

        public ActionResult CheckServer()
        {
            GenericModel _genericModel = new GenericModel(3000, "Server is Live");
            return Json(_genericModel);
        }

        [Authorize]
        public ActionResult IsAuthorized()
        {
            GenericModel _genericModel = new GenericModel(0000, "Authorized");
            return Json(_genericModel);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要覆盖以下两种方法 -

  • ValidateUser - 需要验证。
  • GetUser - 调用它以便在向服务器发出请求时创建 IPrincipal 对象。

自定义成员资格提供程序

public class CustomMembershipProvider : MembershipProvider
{    
   public override bool ValidateUser(string username, string password)
   {            
   }

   public override MembershipUser GetUser(string username, bool userIsOnline)
   {           
   }
}

自定义角色提供程序

如果您在登录后需要授权,则还需要实施角色提供程序。

public class CustomRoleProvider : RoleProvider
{
   public override bool IsUserInRole(string username, string roleName)
   {
   }

   public override string[] GetRolesForUser(string username)
   {
   } 
}