我目前正在使用EpiServer CMS 8的網站上工作,並用Owin/ASP.Net身份替換登錄。ASP.Net身份與episerver不工作時部署
在本地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 guide for startup handling上列出的所有內容。該OwinMembershipProvder和OwinRoleProvider是基於從http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver的代碼,但擴展和修改,以適應我們的要求