简体   繁体   English

html5本地应用程序可以有一个asp.net会话吗? (适用于iPhone的本地webapp)

[英]Can a html5 local app have an asp.net session? (local webapp for iPhone)

The context: 上下文:

I'm actually developing a small web app (C#/MVC2). 我实际上正在开发一个小型Web应用程序(C#/ MVC2)。 Users are going to use their iPhones (and probably Android phones in the future) to access it. 用户将使用他们的iPhone(以及未来的Android手机)来访问它。

At the moment it's quite simple (it just shows some info and reports from our customer's ERP), and I decided to give a try at creating local webapp that the users could add to their iPhones, so that they had an icon for it and, most importantly, most files are locally cached, so that only the relevant data is obtained using json from the server. 目前这很简单(它只显示了我们客户ERP的一些信息和报告),我决定尝试创建用户可以添加到他们的iPhone的本地webapp,以便他们有一个图标,并且,最重要的是,大多数文件都是本地缓存的,因此只使用服务器中的json获取相关数据。

The problem: 问题:

To authenticate users, a small form asks for username and password, and sends them to the server via ajax, which in turn validates the user and sets the authcookie. 要对用户进行身份验证,小表单会要求输入用户名和密码,并通过ajax将它们发送到服务器,而jax会对用户进行验证并设置authcookie。 If the app is executed in Safari, everything works ok, but if it's executed locally (that is, in Mobile Safari directly from an icon), the server validates correctly the user, but this validation is lost when the next ajax call to recover data is made. 如果应用程序在Safari中执行,一切正常,但如果它在本地执行(即直接从图标在Mobile Safari中),服务器会正​​确验证用户,但是当下一次ajax调用恢复数据时,此验证将丢失是。

Does this mean that session cookies are not supported by Mobile Safari in webapps? 这是否意味着Web应用程序中的Mobile Safari不支持会话cookie? I'm doing it wrong? 我做错了?

And most importantly: What's the best way to authenticate users in a local webapp that access remote data? 最重要的是: 在访问远程数据的本地Web应用中对用户进行身份验证的最佳方法是什么?

Your best bet : http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api 您最好的选择: http//www.asp.net/web-api/overview/security/individual-accounts-in-web-api

To access a protected resource, the client includes the access token in the Authorization header of the HTTP request 要访问受保护资源,客户端在HTTP请求的Authorization标头中包含访问令牌

Login : 登录 :

var loginData = {
    grant_type: 'password',
    username: ...,
    password: ...
};

$.ajax({
    type: 'POST',
    url: '/Token',
    data: loginData
}).done(function (data) {
    // Cache the access token in session storage.
    sessionStorage.setItem(tokenKey, data.access_token);
});

Second request: 第二个请求:

// If we already have a bearer token, set the Authorization header.
var token = sessionStorage.getItem(tokenKey);
var headers = {};
if (token) {
    headers.Authorization = 'Bearer ' + token;
}

$.ajax({
type: 'GET',
url: 'api/values/1',
headers: headers
}).done(function (data) {});

If you don't plan to use Web API, you must generate your own token and put it in every request data 如果您不打算使用Web API,则必须生成自己的令牌并将其放入每个请求数据中

I'm not quite sure about what do you mean by local webapp. 我不太确定你的本地webapp是什么意思。 I assume that it's an HTTP web server running on localhost. 我假设它是在localhost上运行的HTTP Web服务器。

If that's the case, you need some protocol to communicate between http://localhost and http://yourwebsite.com , and that protocol should help localhost authenticate user via yourwebsite.com. 如果是这种情况,您需要一些协议在http:// localhosthttp://yourwebsite.com之间进行通信,该协议应该帮助localhost通过yourwebsite.com对用户进行身份验证。 I think OAuth might be what you're looking for. 我认为OAuth可能就是你要找的东西。

The first time the user access your local webapp, he will be redirected to yourwebsite.com for the authentication. 用户首次访问您的本地Web应用程序时,他将被重定向到yourwebsite.com进行身份验证。 After that, yourwebsite.com will bring him back with an OAuth token. 之后,yourwebsite.com将带回OAuth令牌。 After verifying that token is valid from yourwebsite.com, localhost can serve user on its own. 在从yourwebsite.com验证令牌有效后,localhost可以自行为用户提供服务。

(I realise I'm very late to this question, but anyway…) (我意识到这个问题已经很晚了,但无论如何......)

Mobile Safari employs a slightly different web engine to that used in "home-screen apps" (ie web pages that you bookmark as self-contained icons on the iOS home screen). Mobile Safari采用与“主屏幕应用程序”(即您在iOS主屏幕上标记为自包含图标的网页)中使用的略有不同的Web引擎。

Perhaps the issue you're seeing with cookies comes from that, rather than in Mobile Safari per se ? 也许你用cookies看到的问题来自于它,而不是Mobile Safari 本身 I guess it's easy enough to test: if the app all works OK in Mobile Safari, and not from a home screen icon, there's your answer. 我想这很容易测试:如果应用程序在Mobile Safari中都可以正常工作,而不是来自主屏幕图标,那就是你的答案。

As an alternative take, rather than relying on authentication in the on-line version of the app, another approach that may work for you / your organisation is using the app in an unauthenticated state, but over a VPN for mobile workers? 作为替代方案,而不是依赖于应用程序的在线版本中的身份验证,可能对您/您的组织有效的另一种方法是在未经身份验证的状态下使用该应用程序,而是通过VPN为移动工作人员使用? (This will still work OK as an offline web app). (这仍然可以作为离线Web应用程序正常工作)。

Instead of using a cookie can't you have a ajax call to login that just returns the "authcookie"-value. 你没有使用cookie就不能对登录进行ajax调用,只返回“authcookie”值。 The value can be saved using localStorage or similar. 可以使用localStorage或类似方法保存该值。

http://dev.w3.org/html5/webstorage/ http://dev.w3.org/html5/webstorage/

Later when you want to fetch something you can send this value to the server using a custom header (X-authentication or similar) or just append it as a GET-variable to the url. 稍后当您想要获取某些内容时,您可以使用自定义标头(X身份验证或类似)将此值发送到服务器,或者只是将其作为GET变量附加到URL。

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

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