0

我有一个 Windows 窗体应用程序,我将其配置为与 ASP.NET 服务一起使用,该服务是使用自定义 MembershipProvider 的身份验证服务。

当我在 Windows 应用程序中调用 Membership.CreateUser 时,抛出 NotSupportedException 告诉:“不支持指定的方法。”。

我尝试在网站中创建一个网页并测试 MembershipProvider,在网站内工作时一切正常。

任何关于如何使用自定义(不是 SqlMembershipProvider)MembershipProvider 的想法或链接将不胜感激!

编辑: ValidateUser 方法确实有效。覆盖的 CreateUser 不起作用我尝试了 Override Sub CreateUser(.......) As MembershipUser Return New User() End Sub 但我仍然得到与以前相同的异常。

4

4 回答 4

2

客户端应用程序服务不支持 CreateUser 方法。

这是来自 ClientFormsAuthenticationMembershipProvider 的 CreateUser 方法源代码:

public override MembershipUser CreateUser
(
    string username, 
    string password, 
    string email,
    string passwordQuestion,
    string passwordAnswer,
    bool isApproved,
    object providerUserKey,
    out MembershipCreateStatus status
)
{
    throw new NotSupportedException();
}
于 2009-12-03T11:10:04.057 回答
0

我认为自定义成员资格提供程序是在实现 ASP.NET AJAX 服务(客户端应用程序服务)时。对于简单的 Windows 应用程序My.User.CurrentPrinciple可用于进行身份验证。

于 2009-12-04T01:21:27.637 回答
0

您将要在您的自定义提供程序上构建一个 json 代理。需要牢记的是保护代理端点!保护代理端点!

我没有测试过这个类中的每一个方法,但是以前的实现表现得很好,所以它最终可能会让你到达你需要去的地方。

保护端点!

// <copyright project="Salient.ScriptModel" file="MembershipService.svc" company="Sky Sanders">
// This source is a Public Domain Dedication.
// http://skysanders.net/subtext
// Attribution is appreciated
// </copyright>
// <version>1.0</version>
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web.Security;

namespace Salient.ScriptModel
{
    [ServiceContract(Namespace = "Salient.ScriptModel", Name = "MembershipProvider")]
    public interface IMembershipProvider
    {
        string ProviderName { get; set; }

        [OperationContract]
        DateTime TestInput(DateTime date);

        [OperationContract]
        ProviderProperties GetProviderProperties();

        [OperationContract]
        MembershipUser GetUserByKey(object providerUserKey, bool userIsOnline);

        [OperationContract]
        MembershipCreateResult CreateUser(string username, string password,
                                          string email, string passwordQuestion,
                                          string passwordAnswer, bool isApproved,
                                          object providerUserKey);

        [OperationContract]
        MembershipCreateResult AddUser(MembershipUser user);

        [OperationContract]
        bool ChangePasswordQuestionAndAnswer(string username, string password,
                                             string newPasswordQuestion, string newPasswordAnswer);

        [OperationContract]
        string GetPassword(string username, string answer);

        [OperationContract]
        bool ChangePassword(string username, string oldPassword, string newPassword);

        [OperationContract]
        string ResetPassword(string username, string answer);

        [OperationContract]
        void UpdateUser(MembershipUser user);

        [OperationContract]
        bool ValidateUser(string username, string password);

        [OperationContract]
        bool UnlockUser(string userName);

        [OperationContract]
        MembershipUser GetUserByName(string username, bool userIsOnline);

        [OperationContract]
        string GetUserNameByEmail(string email);

        [OperationContract]
        bool DeleteUser(string username, bool deleteAllRelatedData);

        [OperationContract]
        MembershipFindResult GetAllUsers(int pageIndex, int pageSize);

        [OperationContract]
        int GetNumberOfUsersOnline();

        [OperationContract]
        MembershipFindResult FindUsersByName(string usernameToMatch, int pageIndex,
                                             int pageSize);

        [OperationContract]
        MembershipFindResult FindUsersByEmail(string emailToMatch, int pageIndex,
                                              int pageSize);
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MembershipProvider : IMembershipProvider
    {
        private System.Web.Security.MembershipProvider Provider
        {
            get
            {
                if (string.IsNullOrEmpty(ProviderName))
                {
                    return Membership.Provider;
                }
                return Membership.Providers[ProviderName];
            }
        }

        #region IMembershipProvider Members

        public DateTime TestInput(DateTime date)
        {
            return date;
        }

        public string ProviderName { get; set; }

        public ProviderProperties GetProviderProperties()
        {
            var returnValue = new ProviderProperties
                                  {
                                      ApplicationName = Provider.ApplicationName,
                                      EnablePasswordReset = Provider.EnablePasswordReset,
                                      EnablePasswordRetrieval = Provider.EnablePasswordRetrieval,
                                      MaxInvalidPasswordAttempts = Provider.MaxInvalidPasswordAttempts,
                                      MinRequiredNonAlphanumericCharacters =
                                          Provider.MinRequiredNonAlphanumericCharacters,
                                      MinRequiredPasswordLength = Provider.MinRequiredPasswordLength,
                                      PasswordAttemptWindow = Provider.PasswordAttemptWindow,
                                      PasswordFormat = Provider.PasswordFormat,
                                      PasswordStrengthRegularExpression = Provider.PasswordStrengthRegularExpression,
                                      RequiresQuestionAndAnswer = Provider.RequiresQuestionAndAnswer,
                                      RequiresUniqueEmail = Provider.RequiresUniqueEmail
                                  };
            return returnValue;
        }

        public MembershipUser GetUserByKey(object providerUserKey, bool userIsOnline)
        {
            return Provider.GetUser(providerUserKey, userIsOnline);
        }

        public MembershipCreateResult CreateUser(string username, string password,
                                                 string email, string passwordQuestion,
                                                 string passwordAnswer, bool isApproved,
                                                 object providerUserKey)
        {
            MembershipCreateStatus status;
            MembershipUser user = Provider.CreateUser(username, password, email, passwordQuestion, passwordAnswer,
                                                      isApproved,
                                                      providerUserKey, out status);
            return new MembershipCreateResult(user, status);
        }

        public MembershipCreateResult AddUser(MembershipUser user)
        {
            return new MembershipCreateResult(user, MembershipCreateStatus.DuplicateEmail);
        }

        public bool ChangePasswordQuestionAndAnswer(string username, string password,
                                                    string newPasswordQuestion, string newPasswordAnswer)
        {
            return Provider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
        }

        public string GetPassword(string username, string answer)
        {
            return Provider.GetPassword(username, answer);
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return Provider.ChangePassword(username, oldPassword, newPassword);
        }

        public string ResetPassword(string username, string answer)
        {
            return Provider.ResetPassword(username, answer);
        }

        public void UpdateUser(MembershipUser user)
        {
            Provider.UpdateUser(user);
        }

        public bool ValidateUser(string username, string password)
        {
            return Provider.ValidateUser(username, password);
        }

        public bool UnlockUser(string userName)
        {
            return Provider.UnlockUser(userName);
        }

        public MembershipUser GetUserByName(string username, bool userIsOnline)
        {
            return Provider.GetUser(username, userIsOnline);
        }

        public string GetUserNameByEmail(string email)
        {
            return Provider.GetUserNameByEmail(email);
        }

        public bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return Provider.DeleteUser(username, deleteAllRelatedData);
        }

        public MembershipFindResult GetAllUsers(int pageIndex, int pageSize)
        {
            int totalRecords;
            MembershipUserCollection users = Provider.GetAllUsers(pageIndex, pageSize, out totalRecords);
            var list = new List<MembershipUser>();
            foreach (MembershipUser user in users)
            {
                list.Add(user);
            }
            return new MembershipFindResult(list, totalRecords);
        }

        public int GetNumberOfUsersOnline()
        {
            return Provider.GetNumberOfUsersOnline();
        }

        public MembershipFindResult FindUsersByName(string usernameToMatch, int pageIndex,
                                                    int pageSize)
        {
            int totalRecords;
            MembershipUserCollection users = Provider.FindUsersByName(usernameToMatch, pageIndex, pageSize,
                                                                      out totalRecords);
            var list = new List<MembershipUser>();
            foreach (MembershipUser user in users)
            {
                list.Add(user);
            }
            return new MembershipFindResult(list, totalRecords);
        }

        public MembershipFindResult FindUsersByEmail(string emailToMatch, int pageIndex,
                                                     int pageSize)
        {
            int totalRecords;
            MembershipUserCollection users = Provider.FindUsersByEmail(emailToMatch, pageIndex, pageSize,
                                                                       out totalRecords);
            var list = new List<MembershipUser>();
            foreach (MembershipUser user in users)
            {
                list.Add(user);
            }
            return new MembershipFindResult(list, totalRecords);
        }

        #endregion
    }

    [DataContract]
    public class MembershipFindResult
    {
        [DataMember] public int RecordCount;
        [DataMember] public IEnumerable<MembershipUser> Users;

        public MembershipFindResult()
        {
        }

        public MembershipFindResult(IEnumerable<MembershipUser> users, int recordCount)
        {
            Users = users;
            RecordCount = recordCount;
        }
    }

    [DataContract]
    public class MembershipCreateResult
    {
        [DataMember] public MembershipCreateStatus CreateStatus;
        [DataMember] public MembershipUser User;

        public MembershipCreateResult()
        {
        }

        public MembershipCreateResult(MembershipUser user, MembershipCreateStatus createStatus)
        {
            User = user;
            CreateStatus = createStatus;
        }
    }

    [DataContract]
    public class ProviderProperties
    {
        public ProviderProperties()
        {
        }

        public ProviderProperties(bool enablePasswordRetrieval, bool enablePasswordReset, bool requiresQuestionAndAnswer,
                                  int maxInvalidPasswordAttempts,
                                  int passwordAttemptWindow, bool requiresUniqueEmail,
                                  MembershipPasswordFormat passwordFormat, int minRequiredPasswordLength,
                                  int minRequiredNonAlphanumericCharacters,
                                  string passwordStrengthRegularExpression, string applicationName)
        {
            EnablePasswordRetrieval = enablePasswordRetrieval;
            EnablePasswordReset = enablePasswordReset;
            RequiresQuestionAndAnswer = requiresQuestionAndAnswer;
            MaxInvalidPasswordAttempts = maxInvalidPasswordAttempts;
            PasswordAttemptWindow = passwordAttemptWindow;
            RequiresUniqueEmail = requiresUniqueEmail;
            PasswordFormat = passwordFormat;
            MinRequiredPasswordLength = minRequiredPasswordLength;
            MinRequiredNonAlphanumericCharacters = minRequiredNonAlphanumericCharacters;
            PasswordStrengthRegularExpression = passwordStrengthRegularExpression;
            ApplicationName = applicationName;
        }

        [DataMember]
        public bool EnablePasswordRetrieval { get; set; }

        [DataMember]
        public bool EnablePasswordReset { get; set; }

        [DataMember]
        public bool RequiresQuestionAndAnswer { get; set; }

        [DataMember]
        public int MaxInvalidPasswordAttempts { get; set; }

        [DataMember]
        public int PasswordAttemptWindow { get; set; }

        [DataMember]
        public bool RequiresUniqueEmail { get; set; }

        [DataMember]
        public MembershipPasswordFormat PasswordFormat { get; set; }

        [DataMember]
        public int MinRequiredPasswordLength { get; set; }

        [DataMember]
        public int MinRequiredNonAlphanumericCharacters { get; set; }

        [DataMember]
        public string PasswordStrengthRegularExpression { get; set; }

        [DataMember]
        public string ApplicationName { get; set; }
    }

}

编辑:必须删除评论以适应。密切关注我的博客以获得完整的实施

ps 保护这个端点!

于 2010-02-24T19:23:53.530 回答
-1

此页面显示如何实现自定义成员资格提供程序。不过,我不确定它是否有助于解决您的问题。

于 2009-08-13T05:39:07.680 回答