1

我目前正在使用 EpiServer CMS 8 的站点上工作,并将登录名替换为 Owin/ASP.Net Identity。

在本地 IIS 上一切正常,但是当部署到我们的测试服务器时,导航到 /episerver/ 不会重定向到登录页面,而是直接给出 401.2 未经授权的结果。

下面是我的启动课

[assembly: OwinStartup(typeof(Website.Startup))]
namespace Website
{
    public class Startup
    {
        private const string PathRoot = "~/";
        private const string LogoutUrl = "/Account/Logout";
        private const string LoginUrl = "/Account/Login";
        private const string BackendLoginUrl = "~/BackendAccount/";
        private const string BackendLogoutUrl = "~/Util/Logout.aspx";

        public void ConfigureAuth(IAppBuilder app)
        {
            Configuration(app);
        }
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString(VirtualPathUtility.ToAbsolute(LoginUrl)),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
                    OnApplyRedirect = ApplyRedirect
                }
            }, PipelineStage.Authenticate);

          app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            app.Map(VirtualPathUtility.ToAbsolute(LogoutUrl), map =>
            {
                map.Run(ctx =>
                {
                    ctx.Authentication.SignOut();
                    return Task.Run(() => ctx.Response.Redirect(VirtualPathUtility.ToAbsolute(PathRoot)));
                });
            });

            app.Map(VirtualPathUtility.ToAbsolute(BackendLogoutUrl), map =>
            {
                map.Run(ctx =>
                {
                    ctx.Authentication.SignOut();
                    return Task.Run(() => ctx.Response.Redirect(VirtualPathUtility.ToAbsolute(PathRoot)));
                });
            });
            app.UseStageMarker(PipelineStage.MapHandler);
        }
        private static void ApplyRedirect(CookieApplyRedirectContext context)
        {
            string backendPath = Paths.ProtectedRootPath.TrimEnd('/');

            if (context.Request.Uri.AbsolutePath.StartsWith(backendPath, StringComparison.CurrentCultureIgnoreCase) && !context.Request.User.Identity.IsAuthenticated)
            {
                context.RedirectUri = VirtualPathUtility.ToAbsolute(BackendLoginUrl) +
                        new QueryString(
                            context.Options.ReturnUrlParameter,
                            context.Request.Uri.AbsoluteUri);
            }

            context.Response.Redirect(context.RedirectUri);
        }
    }
}

我的 web.config 包括这些部分

<authentication mode="None">
</authentication>
<membership defaultProvider="OwinMembershipProvider" userIsOnlineTimeWindow="10" hashAlgorithmType="HMACSHA512">
  <providers>
    <clear /
    <add name="OwinMembershipProvider"
         type="Website.Shared.Providers.OwinMembershipProvider"
         enablePasswordRetrival="false"
         enablePasswordReset="true"
         requiresQuestionAndAnswer="false"
         requiresUniqueEmail="false"
         passwordFormat="Hashed"
         passwordStrengthRegularExpression=""
         minRequiredPasswordLength="6"
         minRequiredNonalphanumericCharacters="0"
         connectionString="TestConnection"
         />
      </providers>
</membership>
<roleManager enabled="true" defaultProvider="OwinRoleProvider" cacheRolesInCookie="true">
  <providers>
    <clear />
    <add name="OwinRoleProvider" type="Website.Shared.Providers.OwinRoleProvider"/>
  </providers>
</roleManager>

我尝试比较服务器之间的 iis 设置,但没有发现任何区别。我真的不知道如何解决这个问题,我已经尝试了Owin 启动处理指南中列出的所有内容。OwinMembershipProvder 和 OwinRoleProvider 基于来自http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver的代码,但经过扩展和修改以满足我们的要求

4

1 回答 1

1

通过首先清除 ASP.Net 临时文件然后重新启动站点并将 web.config 设置为来解决此问题

<add key="owin:AutomaticAppStartup" value="true" />
<add key="owin:AppStartup" value="Website.Startup" />

我之前曾测试过我认为这两个值的所有可能组合,但没有成功。

我不能肯定地说,但清除缓存和从 AppStartup 键中删除程序集名称的组合可能是解决方案。

于 2015-10-26T19:04:26.390 回答