我查看了数十份文档,确定正在执行正确的操作,但肯定是有一些小细节我遗漏了。
我想使用自己的登录机制登录我的网站。登录机制位于Identity Server中,用于单点登录。
但我想使用Umbraco的公共访问功能,因此我正在通过更改配置文件来添加来自本地数据库的角色。
<roleManager enabled="true" defaultProvider="UmbracoRoleProvider">
<providers>
<clear />
<add name="UmbracoRoleProvider" type="rcsedWebServiceBLL.RCSEdRoleProvider" />
</providers>
</roleManager>
并且实现RoleProvider
class RCSEdRoleProvider : RoleProvider
{
private string _ApplicationName = "UmbracoRoleProvider";
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get { return _ApplicationName; }
set
{
if(string.IsNullOrEmpty(value))
throw new ProviderException("ApplicationName Cacnnot be Empty");
if(value.Length > 0x100)
throw new ProviderException("provider application name too long");
_ApplicationName = value;
}
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
return UBISRoles.GetRoleList();
}
public override string[] GetRolesForUser(string username)
{
// code to return user role access
try
{
//ool isAuthenticateSession = RCSEd.UolsSecurity.CommonFunctions.CheckAuthenticateSessionOrNot();
if (HttpContext.Current.Session["UOSStudent"] != null)
{
return (String[])HttpContext.Current.Session["userRoles"];
}
else
{
List<String> retval = new List<string>();
retval.Add("Public");
return retval.ToArray();
}
}
catch (Exception ex)
{
// AppLogWriter _objApplog = new AppLogWriter();
// _objApplog.WriteLogMessages(ex.Message.ToString());
//TODO catch Error
List<String> retval = new List<string>();
retval.Add("Public");
return retval.ToArray();
}
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
try
{
foreach (String role in (String[])HttpContext.Current.Session["userRoles"])
{
if (String.Compare(role, roleName, true) == 0)
return true;
}
return false;
}
catch { }
return false;
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
foreach (string val in UBISRoles.GetRoleList())
{
if (val == roleName)
return true;
}
return false;
}
}
class UBISRoles
{
private static string[] allroles = new string[] { //"Public",
"MembershipCandidate",
"MembershipMember"
};
public static string[] GetRoleList()
{
return allroles;
}
public static void SetRoles(DataTable UBIsRolesResultsTBL)
{
List<String> userRoles = new List<string>();
DataRow rec = UBIsRolesResultsTBL.Rows[0];
switch (rec["Membership"].ToString())
{
case "member":
userRoles.Add("MembershipMember");
break;
case "pending member":
userRoles.Add("MembershipPending");//add
break;
case "public":
userRoles.Add("MembershipPublic");//add
break;
}
HttpContext.Current.Session["userRoles"] = userRoles.ToArray();
}
private static void CheckSimpleFieldVal(List<String> userRoles, DataRow rec, string roleName)
{
try
{
if (string.Compare(rec[roleName].ToString(), "YES", true) == 0)
userRoles.Add(roleName);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
private static void CheckSimpleFieldValV2(List<String> userRoles, DataRow rec, string roleName)
{
try
{
if (string.Compare(rec[roleName].ToString(), "1", true) == 0)
userRoles.Add(roleName);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
这样做很好,一切都被加载到umbraco后台的成员组中。
但是,当我尝试实现MembershipProvider时,感觉有些问题。Web配置文件如下:
<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add name="UmbracoMembershipProvider" type="rcsedWebServiceBLL.RCSEdMembershipProvide" />
<add name="UsersMembershipProvider" type="Umbraco.Web.Security.Providers.UsersMembershipProvider, Umbraco" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="4" useLegacyEncoding="true" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />
</providers>
</membership>
而Membershiprovider类看起来像这样:
class RCSEdMembershipProvide : MembershipProvider
{
string connectionStringName;
private string _ApplicationName = "UmbracoMembershipProvider";
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
/* if (config["ConnectionStringName"] != null)
connectionStringName = config["connectionStringName"];*/
}
public override string ApplicationName
{
get { return _ApplicationName; }
set
{
if (string.IsNullOrEmpty(value))
throw new ProviderException("ApplicationName Cacnnot be Empty");
if (value.Length > 0x100)
throw new ProviderException("provider application name too long");
_ApplicationName = value;
}
}
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)
{
try
{
User user = (User)HttpContext.Current.Session["user"];
if (user != null)
return new MembershipUser("RCSEdMembershipProvider", user.DisplayName, username, user.Email, "", "", true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
else
return null;
}
catch
{
return null;
}
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
try
{
User user = (User)HttpContext.Current.Session["user"];
if (user != null)
return new MembershipUser("RCSEdMembershipProvider", user.DisplayName, providerUserKey, user.Email, "", "", true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
else
return null;
}
catch
{
return null;
}
}
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)
{
return true;
}
}
我一直将validateUser设置为true。
因此,当用户登录到我的身份服务器时,我在HttpContext.Current.Session["userRoles"]
中加载用户角色,并限制页面的公共访问,并将用户重定向到正确的页面。
当用户未登录时,会被重定向到登录页面,这很好,但是当用户返回并加载他/她的角色并尝试访问受限页面时,成员资格提供程序ValidateUser
不起作用,用户会一次又一次地被重定向回登录页面。
PS. 我正在使用umbraco 7.1.3
请原谅我的冗长问题,但我想涵盖所有步骤。谢谢您的帮助。