簡體   English   中英

MVC3應用程序的自定義登錄

[英]Custom Login for MVC3 application

我想建立一個顯示用戶特定信息的網站(例如,用戶登錄時看到自己的記錄)。 我不想將MVC3 Internet應用程序與默認成員資格提供程序一起使用,因為我想體驗自己的身份驗證。 我在網上找到了一些鏈接,但覺得我需要更多的信息來閱讀。 我的項目是MVC3,首先使用代碼的實體框架。 謝謝,

我最近做了類似的事情-我們有一個用經典ASP編寫的應用程序,我想同時使用現有數據庫表通過.NET進行身份驗證(對於感興趣的人,我首先將用戶登錄到經典ASP端,然后發布他們的.NET登錄表單的詳細信息,使它執行自己的登錄過程)。

我發現的幾乎所有建議都建議編寫自己的MemberShip Provider(由(dknaack)和IPrincipal對象建議,以允許我添加所需的其他字段(比擴展配置文件數據容易)。

最終,這實際上非常容易。 我不必費心實現所有MembershipProvider函數,因為我不需要它們。 在這里,我僅使用實體框架來檢查用戶名/密碼是否有效,但顯然您可以使用所需的任何名稱。

這是我的代碼轉儲,以幫助您入門:

using System;
using System.Collections.Specialized;
using System.Linq;
using System.Security.Principal;
using System.Web.Security;
using My.Company.Project.Entities;

namespace My.Company.Project.Classes
{
    public class ClassicMembershipProvider : MembershipProvider
    {
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
        }

        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 ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

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

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

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

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

        public override bool ValidateUser(string username, string password)
        {
            using (var entities = new ProjectEntities())
            {
                return entities.Buprofile
                           .Where(p => p.profileid == username &&
                                       p.profilepassword == password)
                           .Count() == 1;
            }
        }

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

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

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

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

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            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 MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

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

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

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

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

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

        public override bool RequiresUniqueEmail
        {
            get { return true; }
        }

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

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

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

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

您可以通過在web.config中添加/更新來進行連接:

<membership defaultProvider="ClassicMembershipProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add name="ClassicMembershipProvider" type="My.Company.Project.Classes.ClassicMembershipProvider" applicationName="MyApplication" />
  </providers>
</membership>

我還創建了一個自定義的IPrincipal(基於http://blog.codevelop.dk/post/2007/11/24/ASPNET-20-Forms-authentication-Keeping-it-customized-yet-simple.aspx )在UserData屬性中存儲有關用戶的各種數據位(如上所述,與擴展用戶配置文件相比,這種方式看起來容易得多)。

通過強制轉換User:((MyPrincipal)User).UserData,您可以訪問用戶登錄后的UserData。UserData-我在頁面基類中覆蓋了User屬性,因此我只能使用User。 雖然將來:

/// <summary>
/// Override User and return a MyPrincipal with its additional data
/// </summary>
public new MyPrincipal User
{
    get { return HttpContext.Current.User is MyPrincipal ? (MyPrincipal) HttpContext.Current.User : null; }
}

最終,這完全沒有痛苦,並且比從頭開始編寫自己的麻煩要少得多,但我仍然擁有所需的全部控制權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM