管理员登陆的奇妙旅,
第一站,login.aspx.cs
protected void btnLogin_Click(object sender, System.EventArgs e)
{
BlogInfo currentBlog = Config.CurrentBlog;
string returnUrl = Request.QueryString["ReturnURL"];
if(currentBlog == null || (returnUrl != null && StringHelper.Contains(returnUrl, "HostAdmin", StringComparison.InvariantCultureIgnoreCase)))
{
if(!AuthenticateHostAdmin())
{
log.Warn("HostAdmin 登录失败 " + tbUserName.Text);
Message.Text = "登录失败<br />HostAdmin 登录失败";
return;
}
else
{
ReturnToUrl("~/HostAdmin/Default.aspx");
return;
}
}
else
{
if(SecurityHelper.Authenticate(tbUserName.Text, tbPassword.Text, chkRemember.Checked))
{
ReturnToUrl(currentBlog.AdminHomeVirtualUrl);
return;
}
else
{
// string ff = Config.CurrentBlog.UserName;
string ff = Config.CurrentBlog.Password;
Response.Write(ff);
log.Warn("管理登录失败 " + tbUserName.Text);
Message.Text = "登录失败<br />管理登录失败 ";
}
}
}
第二站 在Subtext.Framework里的Security里的
SecurityHelper.cs
public static bool Authenticate(string username, string password, bool persist)
{
if (!IsValidUser(username, password))
{
return false;
}
log.Debug("SetAuthenticationTicket-Admins for " + username);
SetAuthenticationTicket(username, persist, "Admins");
return true;
}
第三站 在Subtext.Framework里的Security里的
SecurityHelper.cs
public static bool IsValidUser(string username, string password)
{
if (String.Equals(username, Config.CurrentBlog.UserName, StringComparison.InvariantCultureIgnoreCase))
{
return IsValidPassword(password);
}
else
{
log.DebugFormat("The supplied username '{0}' does not equal the configured username of '{1}'.", username, Config.CurrentBlog.UserName);
}
return false;
}
第四站 在Subtext.Framework里的Configuration里的
Config.cs
public static BlogInfo CurrentBlog
{
get
{
return ConfigurationProvider.GetBlogInfo();
}
}
第五站 在Subtext.Framework里的Configuration里的
Config.cs
public virtual BlogInfo GetBlogInfo()
{
// First check the context for an existing BlogConfig. This saves us the trouble
// of having to figure out which blog we are at.
BlogInfo info = (BlogInfo) HttpContext.Current.Items[cacheKey];
#if DEBUG
if(info != null)
log.DebugFormat("Info found in the HttpContext.Current.Items cache with cachekey {0}", cacheKey);
#endif
if(info == null)
{
BlogRequest blogRequest = (BlogRequest)HttpContext.Current.Items["Subtext__CurrentRequest"];
//BlogConfig was not found in the context. It could be in the current cache.
string mCacheKey = cacheKey + blogRequest.Subfolder;
//check the cache.
info = (BlogInfo)HttpContext.Current.Cache[mCacheKey];
if(info == null)
{
//Not found in the cache
log.DebugFormat("Attempting to get blog info. Host: {0}, Subfolder: {1}", blogRequest.Host, blogRequest.Subfolder);
info = Config.GetBlogInfo(blogRequest.Host, blogRequest.Subfolder, false);
if(info == null)
{
log.InfoFormat("No active blog found for Host: {0}, Subfolder: {1}", blogRequest.Host, blogRequest.Subfolder);
bool anyBlogsExist = Config.BlogCount > 0;
if (anyBlogsExist && ConfigurationManager.AppSettings["AggregateEnabled"] == "true")
{
return GetAggregateBlog();
}
if(InstallationManager.IsOnLoginPage)
{
return null;
}
throw new BlogDoesNotExistException(blogRequest.Host, blogRequest.Subfolder, anyBlogsExist);
}
if(!info.IsActive && !InstallationManager.IsInHostAdminDirectory && !InstallationManager.IsInSystemMessageDirectory && !InstallationManager.IsOnLoginPage)
{
throw new BlogInactiveException();
}
BlogConfigurationSettings settings = Config.Settings;
// look here for issues with gallery images not showing up.
string webApp = HttpContext.Current.Request.ApplicationPath;
if(webApp.Length <= 1)
webApp="";
string formattedHost = GetFormattedHost(blogRequest.Host, settings.UseWWW) + webApp;
string subfolder = blogRequest.Subfolder;
if(!subfolder.EndsWith("/"))
{
subfolder += "/";
}
if(subfolder.Length > 1)
subfolder = "/" + subfolder;
string virtualPath = string.Format(CultureInfo.InvariantCulture, "images/{0}{1}month_{2}/", Regex.Replace(blogRequest.Host + webApp, @"\:|\.", "_"), subfolder, DateTime.Now.ToString("yyMM"));
// now put together the host + / + virtual path (url) to images
info.ImagePath = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", formattedHost, virtualPath);
try
{
info.ImageDirectory = HttpContext.Current.Request.MapPath("~/" + virtualPath);
}
catch(ArgumentNullException nullException)
{
log.Warn("Could not map the image directory.", nullException);
}
CacheConfig(HttpContext.Current.Cache, info, mCacheKey);
HttpContext.Current.Items.Add(cacheKey, info);
if(!InstallationManager.IsInHostAdminDirectory)
{
// Set the BlogId context for the current request.
Log.SetBlogIdContext(info.Id);
}
else
{
Log.ResetBlogIdContext();
}
}
else
{
HttpContext.Current.Items.Add(cacheKey, info);
}
}
//TODO: Use dependency injection or a provider. This'll do for now.
return info;
}
第六站 在Subtext.Framework里的Configuration里的
Config.cs
public static BlogInfo GetBlogInfo(string hostName, string subfolder, bool strict)
{
hostName = BlogInfo.NormalizeHostName(hostName);
return ObjectProvider.Instance().GetBlogInfo(hostName, subfolder, strict);
}
第七站 Subtext.Framework里的Providers里的ObjectProvider.cs
public abstract class ObjectProvider : ProviderBase
{
private static ObjectProvider provider = null;
private static GenericProviderCollection<ObjectProvider> providers = ProviderConfigurationHelper.LoadProviderCollection("ObjectProvider", out provider);
public static ObjectProvider Instance()
{
return provider;
}
public static GenericProviderCollection<ObjectProvider> Providers
{
get
{
return providers;
}
}
第八站 Subtext.Framework里的Data里的DatabaseObjectProvider.cs
public override BlogInfo GetBlogInfo(string hostname, string subfolder, bool strict)
{
IDataReader reader = DbProvider.Instance().GetBlogInfo(hostname, subfolder, strict);
try
{
BlogInfo info = null;
while(reader.Read())
{
info = DataHelper.LoadConfigData(reader);
break;
}
return info;
}
finally
{
reader.Close();
}
}
Subtext.Framework里的Data里DataHelper.cs里
public static BlogInfo LoadConfigData(IDataReader reader)
{
BlogInfo info = new BlogInfo();
info.Author = ReadString(reader, "Author");
info.Id = DataHelper.ReadInt32(reader, "BlogId");
info.Email = ReadString(reader, "Email");
info.Password = ReadString(reader, "Password");
info.SubTitle = ReadString(reader, "SubTitle");
info.Title = ReadString(reader, "Title");
info.UserName = ReadString(reader, "UserName");
info.TimeZoneId = ReadInt32(reader, "TimeZone");
info.ItemCount = ReadInt32(reader, "ItemCount");
info.CategoryListPostCount = ReadInt32(reader, "CategoryListPostCount");
info.Language = ReadString(reader, "Language");
info.PostCount = ReadInt32(reader, "PostCount");
info.CommentCount = ReadInt32(reader, "CommentCount");
info.StoryCount = ReadInt32(reader, "StoryCount");
info.PingTrackCount = ReadInt32(reader, "PingTrackCount");
info.News = ReadString(reader, "News");
info.LastUpdated = ReadDate(reader, "LastUpdated", new DateTime(2003, 1 , 1));
info.Host = ReadString(reader, "Host");
// The Subfolder property is stored in the Application column.
// This is a result of the legacy schema.
info.Subfolder = ReadString(reader, "Application");
info.Flag = (ConfigurationFlag)(ReadInt32(reader, "Flag"));
info.Skin = new SkinConfig();
info.Skin.TemplateFolder = ReadString(reader, "Skin");
info.Skin.SkinStyleSheet = ReadString(reader, "SkinCssFile");
info.Skin.CustomCssText = ReadString(reader, "SecondaryCss");
info.LicenseUrl = ReadString(reader, "LicenseUrl");
info.DaysTillCommentsClose = ReadInt32(reader, "DaysTillCommentsClose", int.MaxValue);
info.CommentDelayInMinutes = ReadInt32(reader, "CommentDelayInMinutes");
info.NumberOfRecentComments = ReadInt32(reader, "NumberOfRecentComments");
info.RecentCommentsLength = ReadInt32(reader, "RecentCommentsLength");
info.FeedbackSpamServiceKey = ReadString(reader, "AkismetAPIKey");
info.FeedBurnerName = ReadString(reader, "FeedBurnerName");
return info;
}
第九站 回到Subtext.Framework里的Providers里的DbProvider.cs
public abstract class DbProvider : ProviderBase
{
private static DbProvider provider;
private static GenericProviderCollection<DbProvider> providers = ProviderConfigurationHelper.LoadProviderCollection("Database", out provider);
/// <summary>
/// Returns the currently configured DbProvider.
/// </summary>
/// <returns></returns>
public static DbProvider Instance()
{
return provider;
}
/// <summary>
/// Returns all the configured DbProvider.
/// </summary>
public static GenericProviderCollection<DbProvider> Providers
{
get
{
return providers;
}
}
第十站Subtext.Extensibility.Providers. ProviderConfigurationHelper
public static GenericProviderCollection<T> LoadProviderCollection<T>(string sectionName, out T provider) where T : ProviderBase
{
// Get a reference to the provider section
ProviderSectionHandler section = (ProviderSectionHandler)WebConfigurationManager.GetSection(sectionName);
// Load registered providers and point _provider
// to the default provider
GenericProviderCollection<T> providers = new GenericProviderCollection<T>();
ProvidersHelper.InstantiateProviders(section.Providers, providers, typeof(T));
provider = providers[section.DefaultProvider];
if (provider == null)
throw new ProviderException(string.Format("Unable to load default '{0}' provider", sectionName));
return providers;
}
第十一站 到Subtext.Framework里的Data里的SqlDataProvider.cs
public override IDataReader GetBlogInfo(string host, string subfolder, bool strict)
{
SqlParameter[] p =
{
DataHelper.MakeInParam("@Host", SqlDbType.NVarChar, 100, host)
,DataHelper.MakeInParam("@Application", SqlDbType.NVarChar, 50, subfolder)
,DataHelper.MakeInParam("@Strict", SqlDbType.Bit, 1, strict)
};
return GetReader("subtext_GetConfig", p);
}