到目前爲止沒有迴應,所以我能做的最好的事情就是確認它確實在身體上有效。有關如何設置Flex Builder來編寫與Django頁面模板通信的html-wrappers的詳細信息,請參閱my other post。以上用的是前述的組合來實現,以及:
製造一個SessionAlias
模型:
class SessionAlias(models.Model):
alias = models.CharField(max_length=40, primary_key=True)
session = models.ForeignKey(Session)
created = models.DateTimeField(auto_now_add=True)
彎曲點到一個Django頁面經由視圖負載包含:
s = SessionAlias()
s.alias = SessionStore().session_key // generates new 40-char random
s.session = Session.objects.get(session_key=request.session.session_key)
s.save();
randomcookie = SessionStore().session_key // generates new 40-char random
kwargs['extra_context']['randomcookie'] = randomcookie
response = direct_to_template(request, **kwargs)
response.set_cookie(randomcookie, value=alias)
在flex html-wrapper,其中randomcookie是查找別名的位置:
<param name="flashVars" value="randomcookie={{randomcookie}}" />
在applicationComplete
,我們得到randomcookie並找到別名,並登錄使用:
var randomcookie:String = this.parameters["randomcookie"];
// randomcookie is something like "abc123"
var js:String = "function get_cookie(){return document.cookie;}";
var cookies:String = ExternalInterface.call(js).toString();
// cookies looks like "abc123=def456; sessionid=ghi789; ..."
var alias:String = // strip out the "def456"
mynetconnection.call("loginByAlias", alias, successFunc, failureFunc);
這反過來又訪問這個PyAMF的網關RPC:
from django.contrib.auth import SESSION_KEY, load_backend
from django.contrib.auth.models import User
from django.contrib import auth
from django.conf import settings
def loginByAlias(request, alias):
a = SessionAlias.objects.get(alias=alias)
session_engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
session_wrapper = session_engine.SessionStore(a.session.session_key)
user_id = session_wrapper.get(SESSION_KEY)
user = User.objects.get(id=user_id)
user.backend='django.contrib.auth.backends.ModelBackend'
auth.login(request, user)
a.delete()
return whateverToFlash
而在這一點上,在flash/flex側,該特定mynetconnection
保留了會話cookie狀態,可以進行未來呼叫,以便在網關內部,request.user
是首先登錄到網頁的正確驗證用戶。
再次請注意,flex的運行/調試設置必須使用https以及NetConnection
的網關設置。當發佈這個時,我必須確保已通過身份驗證的用戶繼續使用https。
任何人的進一步信息,將不勝感激,尤其是如果有對這個安全方面真實的反饋...