简体   繁体   English

带有Episerver的ASP.Net Identity部署后不起作用

[英]ASP.Net Identity with episerver not working when deployed

I'm currently working on a site using EpiServer CMS 8 and replacing the login with Owin/ASP.Net Identity. 我目前正在使用EpiServer CMS 8的站点上进行工作,并将登录名替换为Owin / ASP.Net Identity。

Everything is working fine on local IIS but when deploying to our test server navigating to /episerver/ doesn't redirect to the login page but directly gives a 401.2 unauthorized result. 在本地IIS上一切正常,但是当部署到我们的测试服务器时,导航到/ episerver /不会重定向到登录页面,而是直接给出401.2未经授权的结果。

Below is my startup 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);
        }
    }
}

My web.config includes these sections 我的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>

I've tried to compare iis settings between the servers and I can find no difference. 我试图比较服务器之间的iis设置,但没有发现任何区别。 I really have no clue how to troubleshoot this and I've tried everything listed on the Owin guide for startup handling . 我真的不知道如何解决这个问题,我已经尝试了Owin指南中列出的用于启动处理的所有内容 The OwinMembershipProvder and OwinRoleProvider are based on the code from http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver but extended and modified to fit our requirements OwinMembershipProvder和OwinRoleProvider基于http://www.mogul.com/om-mogul/blogg/owin-membership-and-role-provider-for-episerver中的代码,但经过扩展和修改以符合我们的要求

Solved this by first clearing ASP.Net temporary files and then restarting the site with web.config set as 通过首先清除ASP.Net临时文件,然后将web.config设置为重新启动站点来解决此问题。

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

I had previously tested what I though was every possible combination of these two values without success. 我以前曾经测试过,但这两个值的每种可能组合都没有成功。

I can't say for sure but the combination of clearing the cache and removing the assembly name from the AppStartup key may have been the solution. 我不能肯定地说,但是清除缓存和从AppStartup键中删除程序集名称的组合可能是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM