【问题标题】:IsAuthenticated works on browser - but not with Air client!IsAuthenticated 适用于浏览器 - 但不适用于 Air 客户端!
【发布时间】:2011-09-28 23:50:03
【问题描述】:

我的登录码,认证后:

var authTicket = new FormsAuthenticationTicket(
                1,
                userName,
                DateTime.Now,
                DateTime.Now.AddMinutes(20), // expiry
                false,
                roles,
                "/");
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);

感谢Darin Dimitrov,我有一个自定义的 Authorize 属性:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class TJAuthorize : AuthorizeAttribute {
    public override void OnAuthorization(AuthorizationContext filterContext) {
        string cookieName = FormsAuthentication.FormsCookieName;

        if (!filterContext.HttpContext.User.Identity.IsAuthenticated ||
                filterContext.HttpContext.Request.Cookies == null || filterContext.HttpContext.Request.Cookies[cookieName] == null) {
                    HandleUnauthorizedRequest(filterContext);
            return;
        }

        var authCookie = filterContext.HttpContext.Request.Cookies[cookieName];
        var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        string[] roles = authTicket.UserData.Split(',');

        var userIdentity = new GenericIdentity(authTicket.Name);
        var userPrincipal = new GenericPrincipal(userIdentity, roles);

        filterContext.HttpContext.User = userPrincipal;
        base.OnAuthorization(filterContext);
    }

当我在浏览器会话中工作时,这一切都很好。但现在我正在使用 Flash/Adobe Air 客户端,并且身份验证属性导致失败。通过将调试语句放入代码中,我可以看出:

filterContext.HttpContext.User.Identity.IsAuthenticated

是假的 - 即使在成功登录之后!

为什么使用浏览器客户端和使用 Air 客户端有什么区别?我该如何解决这个问题?

编辑: 另一个线索:在添加更多调试语句后,我发现从 Air 进行调用时 filterContext.HttpContext.User.Identity 设置不正确 - Name 属性显示为空白!会话 ID 正确,cookie ID 正确 - 但未设置 User.Identity。任何想法为什么会发生这种情况?

【问题讨论】:

    标签: asp.net asp.net-mvc air


    【解决方案1】:

    也许 HttpCookieMode (http://msdn.microsoft.com/en-us/library/system.web.httpcookiemode.aspx) 设置为错误值?

    默认是 UseDeviceProfile ...当你强制它使用 UseCookies 时会发生什么?

    【讨论】:

    • 太棒了 - 我在<system.web><authentication><forms> 标记中添加了cookieless="UseCookies",现在它可以工作了!天才,你! :)
    • 耶!!感谢上帝! ......这是一个团队的努力 - 伟大的工作肖尔!感谢@vnuk 也帮助我们朝着正确的方向思考!
    【解决方案2】:

    HttpContext.User.Identity 是否出现在 global.asax 的 Application_AuthorizeRequest 中?

    【讨论】:

    • 与其他地方的行为相同 - 从浏览器调用时,是的;从空中呼叫时,没有。
    【解决方案3】:

    这是一个远景,但 IsAuthenticated 取决于客户端的 ASPXAUTH cookie(或任何您命名的 id)与请求一起发送。确保 flash/air 正在发送该 cookie(通过 wireshark 或任何其他网络工具)

    【讨论】:

    • 是的,好主意,但我正在发送 ASPXAUTH cookie,已检查它是否与身份验证后返回的相同...
    • 空中请求一定有什么不同。使用wireshark 捕获两个请求并进行比较。
    • 我正在与 Shaul 合作...我们使用的是 Charles,Cookie 没有差异(但其他参数存在差异)
    • 查看上面发布的代码,除了 cookie 问题之外的任何其他问题都没有意义,至少对我而言。在每个请求上,aspnet 都必须对请求进行身份验证,并且只能通过 auth cookie 来完成。
    • 这确实令人费解。显然,aspnet 在授权方面存在问题,但如果发送会话和身份验证 cookie,我不知道其他任何事情。当你非常绝望时,下载 .net 调试符号并逐步执行 aspnet 代码。
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    相关资源
    最近更新 更多