我目前正在使用 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的代码,但经过扩展和修改以满足我们的要求