ASP.Net 部署时 episerver 的身份不工作
ASP.Net Identity with episerver not working when deployed
我目前正在使用 EpiServer CMS 8 开发一个站点,并将登录名替换为 Owin/ASP.Net Identity。
在本地 IIS 上一切正常,但是当部署到我们的测试服务器时,导航到 /episerver/ 不会重定向到登录页面,而是直接给出 401.2 未经授权的结果。
下面是我的启动class
[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. The OwinMembershipProvder and OwinRoleProvider are based on the code from http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver 中列出的所有内容,但进行了扩展和修改以满足我们的要求
通过先清除 ASP.Net 临时文件然后再清除来解决这个问题
重新启动站点 web.config 设置为
<add key="owin:AutomaticAppStartup" value="true" />
<add key="owin:AppStartup" value="Website.Startup" />
我之前曾测试过这两个值的所有可能组合,但没有成功。
我不能肯定地说,但清除缓存和从 AppStartup 键中删除程序集名称的组合可能是解决方案。
我目前正在使用 EpiServer CMS 8 开发一个站点,并将登录名替换为 Owin/ASP.Net Identity。
在本地 IIS 上一切正常,但是当部署到我们的测试服务器时,导航到 /episerver/ 不会重定向到登录页面,而是直接给出 401.2 未经授权的结果。
下面是我的启动class
[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. The OwinMembershipProvder and OwinRoleProvider are based on the code from http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver 中列出的所有内容,但进行了扩展和修改以满足我们的要求
通过先清除 ASP.Net 临时文件然后再清除来解决这个问题 重新启动站点 web.config 设置为
<add key="owin:AutomaticAppStartup" value="true" />
<add key="owin:AppStartup" value="Website.Startup" />
我之前曾测试过这两个值的所有可能组合,但没有成功。
我不能肯定地说,但清除缓存和从 AppStartup 键中删除程序集名称的组合可能是解决方案。