【问题标题】:What is the role of the cookie middleware in SPA TemplateSPA模板中cookie中间件的作用是什么
【发布时间】:2015-10-09 11:56:39
【问题描述】:

VS 2013 SPA 模板配置了 cookie 中间件,以及 OAuth MW 或 ExternalCookie MW 等其他中间件。

    // Enable the application to use a cookie to store information for the signed in user
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(20),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });
    // Use a cookie to temporarily store information about a user logging in with a third party login provider
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Enable the application to use bearer tokens to authenticate users
    app.UseOAuthBearerTokens(OAuthOptions);

由于 WebApi 将通过不记名令牌授权从客户端进行的调用,并且外部 cookie 中间件支持外部登录提供程序,因此 cookie 中间件在其中扮演什么角色?

【问题讨论】:

    标签: visual-studio-2013 single-page-application katana owin-middleware


    【解决方案1】:

    它可以替代表单身份验证。以前,用户会使用表单身份验证登录,然后它会发出一个 cookie,并创建一个代表用户身份的主体对象。使用 OWIN,cookie 身份验证中间件执行相同的任务。

    它还将处理诸如登录/重定向(禁止请求)之类的事情,正如您在上面的代码行中看到的那样:

    LoginPath = new PathString("/Account/Login"),
    

    【讨论】:

    • 我的问题专门针对它在 SPA 中所扮演的角色,它到底保护了什么?由于它是一个 spa 应用程序,我真的很关心保护对 web api 的访问,并且由不记名令牌和 oauth 中间件处理,对吗?
    • @Tom - 好吧,您希望在您的 SPA 中使用用户帐户,对吗?如果一个用户不应该拥有查看视图的权限怎么办? API 可以拒绝它们,但您可能希望它们根本看不到它们。
    • 是的,虽然我的访问令牌可能会在 auth cookie 到期之前过期,所以在这些情况下我仍然必须明确处理到登录页面的重定向。 cookie中间件(浏览器)会监控auth cookie的过期时间,这与access token的exp不同。
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    相关资源
    最近更新 更多