gpt4 book ai didi

c# - SiteCore .Net 成员(member)资格示例

转载 作者:行者123 更新时间:2023-11-30 22:14:56 24 4
gpt4 key购买 nike

我使用 SiteCore 7 大约一个月或更短时间。

我无法从其中的外部数据库设置 .Net 成员(member)提供程序。

这是我的类(class)的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;

namespace MProvider
{
public abstract class MemberProvider : MembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
}

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

public override bool ValidateUser(string username, string password)
{
throw new NotImplementedException();
}
}
}
}

这是SiteCore中的web.config

<switchingProviders>
<membership>
<provider providerName="sql" storeFullNames="true" wildcard="%" domains="*"/>
<provider providerName="MProvider" storeFullNames="true" wildcard="%" domains="*"/>

<membership defaultProvider="sitecore" hashAlgorithmType="SHA1">
<providers>
<clear/>
<add name="sitecore" type="Sitecore.Security.SitecoreMembershipProvider, Sitecore.Kernel" realProviderName="MProvider" providerWildcard="%" raiseEvents="true"/>
<add name="sql" type="System.Web.Security.SqlMembershipProvider" connectionStringName="core" applicationName="sitecore" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="256"/>
<add name="MProvider" type="Sitecore.Security.SitecoreMembershipProvider" realProviderName="MProvider"
applicationName="sitecore" minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"></add>
</providers>
</membership>}

我的问题是:

  1. 有人曾在 SiteCore 中与 .Net 成员(member)提供商合作过吗?A。如果是这样,我做错了什么?
  2. 有人知道 SiteCore 中 .Net 成员资格的工作示例吗?

非常感谢!

戴夫

最佳答案

几天前,我实现了成员资格提供程序和角色提供程序,以允许具有特定角色的外部数据库用户登录到 sitecore。这个答案对我帮助很大。 https://stackoverflow.com/a/6954337/421490

这是我的成员(member)提供程序实现。我打扫了这个类。所有其他方法和属性都被覆盖并抛出 NotImplementedException。

public class FrontEndMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
var webSecurity = DependencyResolver.Current.GetService<IWebSecurity>();

var cleanUsername = CleanUsername(this.Name, username);
if (!string.IsNullOrEmpty(cleanUsername))
{
return webSecurity.ValidateUser(cleanUsername, password) != null;
}

return false;
}

public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();

var user = usersRepository.GetById((int)providerUserKey);
if (user == null)
{
return null;
}

return this.CreateMembershipUser(user);
}

public override MembershipUser GetUser(string username, bool userIsOnline)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();

var cleanUsername = CleanUsername(this.Name, username);
if (!string.IsNullOrEmpty(cleanUsername))
{
var user = usersRepository.GetByEmail(cleanUsername);
if (user != null)
{
return this.CreateMembershipUser(user);
}
}

return null;
}

public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();
var membershipUserCollection = new MembershipUserCollection();

var allUsersWithRole = usersRepository.GetAllUsersForSitecore(pageIndex + 1, pageSize);
foreach (var user in allUsersWithRole)
{
membershipUserCollection.Add(this.CreateMembershipUser(user));
}

totalRecords = allUsersWithRole.TotalItemCount;

return membershipUserCollection;
}

private MembershipUser CreateMembershipUser(User user)
{
return new MembershipUser(this.Name, UpgradeUsername(this.Name, user.Email), user.Id, user.Email, null, null, user.UserAccountStateId == (int)UserAccountStateType.Active, user.UserAccountStateId == (int)UserAccountStateType.Banned, user.CreationDate, user.LastActivityDate ?? user.CreationDate, user.LastActivityDate ?? user.CreationDate, user.CreationDate, user.CreationDate);
}

public static string CleanUsername(string domain, string username)
{
if (!string.IsNullOrEmpty(username))
{
if (username.Contains("\\"))
{
var parts = username.Split('\\');
return parts[0] == domain ? parts[1] : null;
}
}

return username;
}

public static string UpgradeUsername(string domain, string username)
{
if (!string.IsNullOrEmpty(username))
{
if (username.Contains("\\"))
{
var parts = username.Split('\\');
if (parts[0] == domain)
{
return username;
}
}

return domain + "\\" + username;

}

return username;
}

关于c# - SiteCore .Net 成员(member)资格示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com