简体   繁体   English

ASP.NET表单身份验证Cookie和会话以获取其他数据

[英]ASP.NET Forms Authentication Cookie and Sessions for additional data

Application Background Security: The applications are all hosted on a private extranet (and / or a local intranet - depending on the installation instance). 应用程序背景安全性:所有应用程序都托管在专用Extranet(和/或本地Intranet-取决于安装实例)上。 So while security is important, it's not as important as if it were an application on the intranet. 因此,尽管安全很重要,但它并不像Intranet上的应用程序那么重要。 However saying that it is important that the system cannot be easily be hacked or hijacked. 但是要说,重要的是不能轻易地对系统进行黑客攻击或劫持。

The apps: The application comes in 2 parts:- 应用程序:该应用程序分为2部分:-

  • Class Library (dll) 类库(dll)
  • Authentication Front-end ASP.NET application 身份验证前端ASP.NET应用程序

The dll is part of the front-end authentication application, and is to be added to other applications ("consumer apps") that require users to be authenticated. 该dll是前端身份验证应用程序的一部分,并将被添加到其他需要对用户进行身份验证的应用程序(“消费者应用程序”)中。

The authentication app is a central store of all users, applications they have access to and permissions levels based on their username. 身份验证应用程序是所有用户,他们可以访问的应用程序以及基于其用户名的权限级别的集中存储。

For consumer apps that have the dll installed, when an end-user hits a page that requires them to be logged in, the consumer app fires them off to the authentication application login.aspx page along with the appid, the user logs in, if they have required permissions then the auth app, sends them back to the consumer app (via a form with encrypted data) - which includes basic data about who the user is, username, realname, job role, organisation etc... and importantly a list of their permission levels for the consumer app. 对于安装了dll的消费者应用程序,当最终用户点击要求其登录的页面时,消费者应用程序会将其与该appid一起触发到身份验证应用程序login.aspx页面,如果用户登录,他们具有所需的权限,然后是auth应用,然后将它们发送回消费者应用(通过带有加密数据的表单)-其中包括有关用户身份,用户名,真实姓名,工作角色,组织等的基本数据,而且重要的是消费者应用程序的权限级别列表。

The consumer app then takes that data, and processes it, decrypts it etc.. and creates a forms authentication cookie & populates a user class, and user roles class - this is all done from within the dll itself. 然后,消费者应用程序将获取该数据,并对其进行处理,解密等。并创建一个表单身份验证cookie,并填充一个用户类和用户角色类-所有这些都在dll本身内部完成。

The Problem 问题

Now this all works great, and initially all the data was stored in the authentication cookie, in the userdata part of the cookie, however here's the issue.... 现在,这一切都很好,并且最初所有数据都存储在身份验证cookie中,位于cookie的userdata部分中,但这是问题所在。

A consumer app (and there is one central one that we has been written in-house, can have lots of permissions (user roles) associated with a single user (mainly application administrators) and so we need something that can hold lots of data, more than the 4KBs that the authentication cookie can hold. 消费者应用程序(我们在内部编写了一个中心应用程序,可以具有与单个用户(主要是应用程序管理员)相关的许多权限(用户角色),因此我们需要可以容纳大量数据的东西,超过身份验证Cookie可以容纳的4KB。

So I've attempted to put this into Session variables, well initially a single variable with all the sent over decrypted data into a single session variable called "userdata". 因此,我尝试将其放入Session变量中,最初是将单个变量与所有通过解密发送的数据一起放入称为“ userdata”的单个Session变量中。 Which I then check when a requested is made. 然后我检查何时发出请求。

However... 然而...

The first issue I had was that the authentication cookie seems to have a longer life-span than the Session does, I think I've fixed this by extending the session to 35 minutes (5 minutes longer than the AuthCookie). 我遇到的第一个问题是,身份验证Cookie的寿命似乎比Session更长,我认为我已经通过将会话延长到35分钟(比AuthCookie长5分钟)来解决了这一问题。

But when the consumer app programmer makes changes to their code (running localhost in debug via Visual Studio 2010) and refreshes the browser, the AuthCookie remains but the Session disappears. 但是,当消费者应用程序程序员更改其代码(通过Visual Studio 2010在调试中运行localhost)并刷新浏览器时,AuthCookie保留,但Session消失。 Now initially I'm using the default InProc session mode, which I guess could be the issue. 现在,最初我使用的是默认的InProc会话模式,我想可能是问题所在。

Is my assumption correct? 我的假设正确吗? And is there a way of programmatically syncing the session and the AuthCookie? 有没有办法以编程方式同步会话和AuthCookie?

Any other advice on solving this issue? 关于解决此问题还有其他建议吗?

Every time your application refreshes (This is happening when you are changing the code likely), but could happen on the server for various reasons, your user sessions are going to be cleared out. 每次您的应用程序刷新时(可能是在更改代码时发生),但是由于各种原因而可能在服务器上发生时,您的用户会话将被清除。

What you most likely want to do if, I'm reading this correctly, is checking for the existence of the cookie in Session_Start, and refreshing the Session Data so that it gets loaded back into the session. 如果我正确阅读此书,最有可能要做的是检查Session_Start中cookie是否存在,并刷新会话数据,以便将其重新加载到会话中。 The session isn't the most stable thing in the world, and even the Session Timeout isn't always what you think it is . 会话并不是世界上最稳定的事情,甚至会话超时也不总是您认为的那样

If you don't already have one, add a Global.asax to your project. 如果您还没有,请向您的项目添加Global.asax。 If it's c#, edit the Global.asax.cs, or VB, I think it's Global.asax.vb. 如果是c#,请编辑Global.asax.cs或VB,我认为它是Global.asax.vb。

protected void Session_Start(object sender, EventArgs e)
{
// Check for Cookie, if it exists here, then load data into the session here.
}

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

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