问题
When ever i publish my web site with debug="false"
mode and BundleTable.EnableOptimizations = true
the server returns the following exception for bundled scripts and styles while requests are just for non authorized requests! But if I run application with debug="true"
or user is logged in (via forms authentication in release mode) the exception does not occur and every thins works just fine.
I think there should be a relation between bundling and authentication!! as the exception indicates.
What is wrong?
my bundling codes:
public static void RegisterBundles(BundleCollection bundles)
{
StyleBundle bundleStyles = new StyleBundle("~/bundles/styles/");
bundleStyles.Include(
"~" + Paths.Scripts.AdminSkin.css.reset_css,
"~" + Paths.Scripts.AdminSkin.css.common_css,
"~" + Paths.Scripts.AdminSkin.css.form_css,
//do not move this line to blow
"~" + Paths.Scripts.site.css.standard_css,
"~" + Paths.Scripts.AdminSkin.css.standard_css,
"~" + Paths.Scripts.AdminSkin.css.special_pages_css,
//"~" + Paths.Scripts.AdminSkin.css.simple_lists_css,
//"~" + Paths.Scripts.AdminSkin.css.block_lists_css,
//"~" + Paths.Scripts.AdminSkin.css.planning_css,
//"~" + Paths.Scripts.AdminSkin.css.table_css,
//"~" + Paths.Scripts.AdminSkin.css.calendars_css,
//"~" + Paths.Scripts.AdminSkin.css.wizard_css,
//"~" + Paths.Scripts.AdminSkin.css.gallery_css,
"~" + Paths.Scripts.site.css.login_css,
"~" + Paths.Scripts.site.css.site_css,
"~" + Paths.Scripts.site.css.tables_css,
"~" + Paths.Scripts.site.css.standard_fa_css,
"~" + Paths.Scripts.site.css.theme_selector_css,
"~" + Paths.Scripts.jquery_ui_1_9_2.css.redmond.jquery_ui_1_9_2_custom_css,
"~" + Paths.Scripts.ImageGallery.wt_rotator_css,
"~" + Paths.Scripts.ImageGallery.default_css,
"~" + Paths.Scripts.calendar.aqua.theme_css
);
ScriptBundle bundleScripts = new ScriptBundle("~/bundles/scripts/");
bundleScripts.Include(
//Admin Skin Scripts
"~" + Paths.Scripts.AdminSkin.js.old_browsers_js,
"~" + Paths.Scripts.AdminSkin.js.libs.jquery_hashchange_js,
"~" + Paths.Scripts.AdminSkin.js.jquery_accessibleList_js,
"~" + Paths.Scripts.AdminSkin.js.searchField_js,
"~" + Paths.Scripts.AdminSkin.js.common_js,
"~" + Paths.Scripts.AdminSkin.js.standard_js,
"~" + Paths.Scripts.AdminSkin.js.jquery_tip_js,
"~" + Paths.Scripts.AdminSkin.js.jquery_contextMenu_js,
"~" + Paths.Scripts.AdminSkin.js.jquery_modal_js,
"~" + Paths.Scripts.AdminSkin.js.list_js,
//"~" + Paths.Scripts.AdminSkin.js.libs.jquery_dataTables_min_js,
//jquery plug ins
"~" + Paths.Scripts.jquery_ui_1_9_2.js.jquery_ui_1_9_2_custom_js,
"~" + Paths.Scripts.jquery_cookie.jquery_cookie_js,
"~" + Paths.Scripts.site.js.site_js,
"~" + Paths.Scripts.site.js.clock_js,
"~" + Paths.Scripts.site.js.size_js,
"~" + Paths.Scripts.site.js.cookie_manager_js,
//devexpress related
"~" + Paths.Scripts.site.js.devexpress_theme_js,
"~" + Paths.Scripts.site.js.devexpress_controls_js,
//chart scripts
"~" + Paths.Scripts.Highstock_1_2_5.highstock_src_modified_js,
"~" + Paths.Scripts.Highstock_1_2_5.exporting_js,
"~" + Paths.Scripts.Highstock_1_2_5.highcharts_more_js,
"~" + Paths.Scripts.site.js.chart_fa_js,
//calendar scripts
"~" + Paths.Scripts.calendar.jalali_js,
"~" + Paths.Scripts.calendar.calendar_js,
"~" + Paths.Scripts.calendar.calendar_setup_js,
"~" + Paths.Scripts.calendar.calendar_fa_js,
"~" + Paths.Scripts.calendar.calendar_en_js,
//image gallery scripts
"~" + Paths.Scripts.ImageGallery.js.jquery_wt_rotator_min_js,
"~" + Paths.Scripts.ImageGallery.js.jquery_easing_1_3_min_js,
"~" + Paths.Scripts.ImageGallery.js.preview_js
);
if (RequestCachedItems.Setting.UseGoogleMap)
{
}
else
{
//leaflet map script
bundleScripts.Include("~" + Paths.Scripts.leaflet_0_7.leaflet_src_js);
//leaflet map style
bundleStyles.Include("~" + Paths.Scripts.leaflet_0_7.leaflet_css, new CssRewriteUrlTransform());
}
ScriptBundle bundleLiveScripts = new ScriptBundle("~/bundles/livescripts/");
bundleLiveScripts.Include(
"~" + Paths.Scripts.signalr.jquery_signalR_2_0_0_min_js,
"~" + Paths.Scripts.signalr.hubs_js,
"~" + Paths.Scripts.liveControls.connectionManager_js,
"~" + Paths.Scripts.liveControls.liveControls_js,
"~" + Paths.Scripts.liveControls.liveTables_js
);
bundles.Add(bundleStyles);
bundles.Add(bundleScripts);
bundles.Add(bundleLiveScripts);
BundleTable.EnableOptimizations = true;
}
the exception:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.HttpContext.RequestRequiresAuthorization() +18 System.Web.Caching.OutputCacheModule.OnLeave(Object source, EventArgs eventArgs) +9606196
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
回答1:
I've used Application_PostAuthenticateRequest
in Global.ascx.cs
and made HttpContext.Current.User
null. I've commented that line and the problem is solved.
public class Global : HttpApplication
{
FormsAuthenticationTicket ticket;
ExtendedUser extendedUser;
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
//cause of problem
//HttpContext.Current.User = null;
if (FormsAuthentication.CookiesSupported)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
using (UnitOfWork unit = new UnitOfWork())
{
if (ticket.Name == SecurityHelpers.SuperAdminUserName)
{
SecurityHelpers.GetSuperAdminUser(unit);
}
else
{
extendedUser = SecurityHelpers.GetUser(unit, ticket);
}
}
if (extendedUser != null)
{
string[] roles = extendedUser.Group.GroupRoles.Select(a => a.Role.RoleName).ToArray();
HttpContext.Current.User = new GenericPrincipal(new MyFormsIdentity(extendedUser), roles);
}
}
catch (Exception)
{
// Decrypt method failed.
}
}
}
else
{
// throw new HttpException("Cookieless Forms Authentication is not " +
// "supported for this application.");
}
}
}
来源:https://stackoverflow.com/questions/21087388/issue-with-bundling-and-release-mode