将网站重新部署到 IIS 导致已登录的用户无法再次登录?

谢尔比115

问题

我有一个 ASP.NET 站点,当我最初部署该站点时,该站点在登录/注销方面运行良好。当我重新部署新更改(通过替换文件)时,那些已登录(有 cookie)的人不再被识别为已登录。此外,他们无法登录,因为单击登录会将他们带到 azure 登录,然后它会重定向回到主页并没有检测到 cookie,所以它显示他们仍然注销。

仅当我部署到 QA (IIS 6.2) 时,这些事情都不会在 Visual Studio 和 IIS Express 中发生。

相关代码

帐户控制器

[AllowAnonymous]
public void SignIn()
{
    if (Request.IsAuthenticated) { return; }

    HttpContext.GetOwinContext().Authentication.Challenge(
        new AuthenticationProperties() { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType
    );
}

public void SignOut()
{
    if (!Request.IsAuthenticated) { return; }

    // SIGN OUT:
    HttpContext.GetOwinContext().Authentication.SignOut(
        OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType
    );

    // COOKIE: Remove the cookie.
    var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    cookie.Expires = DateTime.UtcNow.AddDays(-1);
    Response.Cookies.Add(cookie);
}

AzureSettings 从 Startup.cs 运行

public static void ConfigureAzure(IAppBuilder app)
{
    // COOKIES: Tells it to use cookies for authentication.
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions()
    {
        // CUSTOMIZE: This is where you would adjust cookie experiation and things of that nature.
    });

    //https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-webapi-openidconnect/
    // OPEN-ID: Handle OpenID stuff.
    var notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
        // REFERENCE: https://russellyoung.net/2015/09/05/mvc-role-based-authorization-with-azure-active-directory-aad/
        AuthorizationCodeReceived = PrincipalService.OnAzureAuthenticationSuccess
    };
    var options = new OpenIdConnectAuthenticationOptions()
    {
        ClientId = ClientID,
        Authority = Authority,
        PostLogoutRedirectUri = PostLogoutRedirectUri,
        Notifications = notifications
    };
    app.UseOpenIdConnectAuthentication(options);
}

上面代码中引用的 PrincipalService.cs

/// <summary>
/// Redirects the user to the /Home/ErrorInfo page.
/// </summary>
public static Task OnAzureAuthenticationFailure(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
{
    context.HandleResponse();
    context.Response.Redirect("/Home/ErrorInfo");
    return Task.FromResult(0);
}

/// <summary>
/// Stores the proper identity cookie (doesn't have customer permissions yet).
/// </summary>
public static Task OnAzureAuthenticationSuccess(AuthorizationCodeReceivedNotification context)
{
    var username = context.AuthenticationTicket.Identity.Name;
    try
    {
        StoreCookie(username);
    }
    catch (DbEntityValidationException ex)
    {
        var errors = ex.EntityValidationErrors.FirstOrDefault()?.ValidationErrors.FirstOrDefault()?.ErrorMessage;
        Logger.Log(Level.Error, "An error occurred while storing authentication cookie.", ex);
    }
    catch (Exception ex)
    {
        Logger.Log(Level.Error, "An error occurred while storing authentication cookie.", ex);
    }
    return Task.FromResult(0);
}

/// <summary>
/// Creates and stores a forms authentication cookie for the user.
/// </summary>
private static void StoreCookie(string username, bool rememberMe = false)
{
    Logger.Log(Level.Info, "Storing Cookie");
    var azureUsers = new AzureUserRepository(new AuthenticationEntities());
    var user = azureUsers.Get(u => u.Username == username);
    if (user == null)
    {
        throw new NullReferenceException();
    }

    // Clear any old existing cookies.
    HttpContext.Current.Request.RemoveFormsAuthCookie();

    // Create the principal from the user object.
    var principal = new PrincipalModel(user);

    // Create and store the cookie in the response.
    var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new JavaScriptConverter[] {
        new ActivityConverter(),
        new RoleConverter(),
        new PrincipalModelConverter()
    });
    HttpContext.Current.Response.AddFormsAuthCookie(
        username: user.Username,
        userData: serializer.Serialize(principal),
        isPersistent: rememberMe
    );
}

概括

  • 将站点重新部署到 QA 会导致仍然登录的用户不再被服务器识别为已登录(因此显示登录链接)。
  • 之后,单击登录会将他们带到 azure 登录,然后将他们重定向到他们未被识别为已登录的主页。
  • 在 Chrome 中删除页面的 cookie 可以解决这个问题,直到下次发生。
  • 仅当我部署到 QA 的 IIS 6.2(我们还没有生产服务器)时,在开发(Visual Studio 和 IIS Express)时才会发生这种情况。

我在做什么导致这种情况?需要做些什么来防止这种情况发生?

谢尔比115

我相信问题已经解决了。不确定我是否 100% 理解为什么,但在上面的代码中,我在添加它之前从请求而不是响应中删除了旧的 cookie。一旦我添加了从响应中删除cookie的代码,它就停止引起该问题。

(注意:如果有人能解释为什么会这样,我很乐意接受他们的回答)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用Maven部署将Java应用程序重新部署到tomcat7

来自分类Dev

Team City:无法重新部署到Tomcat7

来自分类Dev

部署在 IIS 上的网站没有获得域 NT 登录

来自分类Dev

部署的网站无法在IIS中使用

来自分类Dev

将ASP.NET MVC网站部署到IIS 8

来自分类Dev

IIS网站具有双重登录选项

来自分类Dev

在以前的网站已停止的情况下,为什么IIS显示我已登录?

来自分类Dev

用户“ IIS APPPOOL”的登录失败

来自分类Dev

部署到 IIS 时无法运行 CrystalReports

来自分类Dev

IIS 7.5 MVC 5部署的网站将无法连接到SQL Server

来自分类Dev

如何预热IIS中托管的asp.net核心应用程序(a)在网站重启时和(b)在重新部署时?

来自分类Dev

无法摆脱“用户IIS APPPOOL \ NETWORKSERVICE的登录失败”

来自分类Dev

无法摆脱“用户IIS APPPOOL \ NETWORKSERVICE的登录失败”

来自分类Dev

将Web应用程序部署到IIS时无法加载母版页

来自分类Dev

获取当前登录的用户IIS,返回DefaultAppPool

来自分类Dev

在IIS 7上让用户登录c#

来自分类Dev

ZSH已删除,无法重新登录

来自分类Dev

将LocalDB部署到IIS7的ASP.net网站

来自分类Dev

如何将ASP Core Web API VueJS网站部署到IIS

来自分类Dev

如何将 ABP 部署到 IIS

来自分类Dev

将网站发布到本地IIS 7.5

来自分类Dev

为什么重新启动客户端IE更改了IIS中登录的用户名?

来自分类Dev

捕获导致 IIS 网站崩溃的 NullReferenceException

来自分类Dev

将3个应用程序移动到一个文件夹后无法重新部署

来自分类Dev

将3个应用程序移动到一个文件夹后无法重新部署

来自分类Dev

IIS网站将无法打开.htm或php文件

来自分类Dev

将远程(主)分支重新部署到特定提交

来自分类Dev

VSTS:发布管理,将构件部署到内部部署的IIS

来自分类Dev

每次我们重新部署站点时,IIS 都会关闭 Windows 身份验证

Related 相关文章

  1. 1

    无法使用Maven部署将Java应用程序重新部署到tomcat7

  2. 2

    Team City:无法重新部署到Tomcat7

  3. 3

    部署在 IIS 上的网站没有获得域 NT 登录

  4. 4

    部署的网站无法在IIS中使用

  5. 5

    将ASP.NET MVC网站部署到IIS 8

  6. 6

    IIS网站具有双重登录选项

  7. 7

    在以前的网站已停止的情况下,为什么IIS显示我已登录?

  8. 8

    用户“ IIS APPPOOL”的登录失败

  9. 9

    部署到 IIS 时无法运行 CrystalReports

  10. 10

    IIS 7.5 MVC 5部署的网站将无法连接到SQL Server

  11. 11

    如何预热IIS中托管的asp.net核心应用程序(a)在网站重启时和(b)在重新部署时?

  12. 12

    无法摆脱“用户IIS APPPOOL \ NETWORKSERVICE的登录失败”

  13. 13

    无法摆脱“用户IIS APPPOOL \ NETWORKSERVICE的登录失败”

  14. 14

    将Web应用程序部署到IIS时无法加载母版页

  15. 15

    获取当前登录的用户IIS,返回DefaultAppPool

  16. 16

    在IIS 7上让用户登录c#

  17. 17

    ZSH已删除,无法重新登录

  18. 18

    将LocalDB部署到IIS7的ASP.net网站

  19. 19

    如何将ASP Core Web API VueJS网站部署到IIS

  20. 20

    如何将 ABP 部署到 IIS

  21. 21

    将网站发布到本地IIS 7.5

  22. 22

    为什么重新启动客户端IE更改了IIS中登录的用户名?

  23. 23

    捕获导致 IIS 网站崩溃的 NullReferenceException

  24. 24

    将3个应用程序移动到一个文件夹后无法重新部署

  25. 25

    将3个应用程序移动到一个文件夹后无法重新部署

  26. 26

    IIS网站将无法打开.htm或php文件

  27. 27

    将远程(主)分支重新部署到特定提交

  28. 28

    VSTS:发布管理,将构件部署到内部部署的IIS

  29. 29

    每次我们重新部署站点时,IIS 都会关闭 Windows 身份验证

热门标签

归档