如果您对c#–ASP.NETSession和LINQ感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于c#–ASP.NETSession和LINQ的详细内容,我们还将为您解答a
如果您对c# – ASP.NET Session和LINQ感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于c# – ASP.NET Session和LINQ的详细内容,我们还将为您解答asp.net session用法的相关问题,并且为您提供关于Asp.net Core 源码-SessionExtensions、ASP.NET Session and Forms Authentication and Session Fixation、ASP.NET Session 清除、ASP.NET WebService 中使用 ASP.NET_SessionId的有价值信息。
本文目录一览:- c# – ASP.NET Session和LINQ(asp.net session用法)
- Asp.net Core 源码-SessionExtensions
- ASP.NET Session and Forms Authentication and Session Fixation
- ASP.NET Session 清除
- ASP.NET WebService 中使用 ASP.NET_SessionId
c# – ASP.NET Session和LINQ(asp.net session用法)
我有一个关于我正在进行的项目的问题.
我有这行代码:
var query = from user in dwe.UsersTable where user.LoginName.Equals(usernameBox.Text) && user.Password.Equals(pwBox.Text) select user; if (query.Count() == 1) { Session["User"] = usernameBox.Text; Response.Redirect("Edit.aspx"); } else { LabelError.Text = "Error try again"; } }
在我的“UsersTable”中,我有一个名为“UserID”的coulmn.我想将“userID”作为会话发送到重定向页面(Edit.aspx),userID必须等于用户名和密码之间的比较结果.
干杯:-)
解决方法
var query = (from user in dwe.UsersTable where user.LoginName.Equals(usernameBox.Text) && user.Password.Equals(pwBox.Text) select user).FirstOrDefault(); if(query!=null) { Session["User"] = query.UserID; Response.Redirect("Edit.aspx"); } else { LabelError.Text = "Error try again"; }
不需要编写使用Count方法的代码而不是这样只需使用FirstOrDefault就可以轻松地获得结果.
Asp.net Core 源码-SessionExtensions
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace SportsStore.Infrastructure {
public static class SessionExtensions {
public static void SetJson(this ISession session, string key, object value) {
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetJson<T>(this ISession session, string key) {
var sessionData = session.GetString(key);
return sessionData == null
? default(T) : JsonConvert.DeserializeObject<T>(sessionData);
}
}
}
ASP.NET Session and Forms Authentication and Session Fixation
https://peterwong.net/blog/asp-net-session-and-forms-authentication/
The title can be misleading, because in concept, one is not related to the other. However, a lot of web applications mix them up, causing bugs that are hard to troubleshoot, and, at worst, causing security vulnerabilities.
A little bit of background on each one. ASP.NET sessions are used to keep track and keep information related to a “user” session. When a web server is initially accessed by a browser, the server generates a unique session ID, and sends that session ID to the browser as the value of a cookie (the name of the cookie is ASP.NET_SessionId). Along with that session ID, a dictionary of objects on the server, often referred to as session state, is allocated corresponding to that session ID. This dictionary can be used to keep track of information unique to that session. For example, it could be used to keep track of items placed in a shopping cart metaphor.
Note that this “session” can exist even if the user has not authenticated. And this is often useful. In a retail web site (like Amazon), you can put items in your shopping cart, and only need to authenticate or sign on when you are ready to checkout — and even then, you can actually make a purchase without needing to authenticate, provided, of course, that a valid credit card is used.
Because this “session” is disjoint from authentication, it is better referred to as a “browser” session instead of as a “user” session. In a kiosk environment, if a user walks away from the kiosk while there are items in a shopping cart, the next user to use the kiosk will still see the same shopping cart. The web server doesn’t know any better that a different user is using the kiosk, because the same session ID is being sent back in the session cookie during interaction with the web server.
That dictionary of objects on the server, the session state, also poses certain complications that most developers are aware of. In a web farm, some form of sticky load balancer has to be used so that session state can be kept in memory. Or a centralized store for the session state is used to make the state consistent across the servers in the web farm. In either case, service performance can be affected. I have a very strong opinion against using session state. I avoid it, if at all possible.
What about Forms Authentication? Forms Authentication is the most common authentication mechanism for ASP.NET web sites. When a user is authenticated, most commonly using a user ID and password, a Forms Authentication cookie is generated and is sent to the browser (the name of the cookie, by default, is .ASPXAUTH). The cookie contains the encrypted form of an authentication ticket that contains, among other things, the user ID that uniquely identifies the user. The same cookie is sent to the web server on each HTTP request, so the web server has an idea of the user identity to correlate to a particular HTTP request.
Everything I mentioned above is common knowledge for web developers. Trouble and confusion only comes about when an expectation is made that an ASP.NET session can be associated with ASP.NET authentication. To be clear, it can be done, but precautionary measures have to be taken.
The problem is related to session hijacking, but better known as session fixation. Assuming that you’ve done your diligence of using SSL/TLS and HttpOnly cookies, there isn’t a big risk of having the session ID stolen/hijacked by sniffing the network. And most applications also perform some session cleanup when the user logs out. Some applications even ensure that a new session ID is created when the user logs in, thinking that this is enough to correlate a session state with a user identity.
Remember that the session cookie and the forms authentication cookie are two different cookies. If the two are not synchronized, the web server could potentially allow or disallow some operations incorrectly.
Here’s a hypothetical (albeit unrealistic) scenario. A banking application puts a savings account balance into session state once the user logs in. Perhaps it is computationally expensive to obtain the account balance, so to improve performance, it is kept at session state. The application ensures that a new session ID is created after the user logs in and clears the session state when the user logs out. This prevents the occurrence of one user reusing the session state of another user. Does it really prevent it? No.
As an end-user having control of my browser, I am privy to the traffic/data that the browser receives. With the appropriate tools like Fiddler2 or Firebug, I can see the session and forms authentication cookies. I may not be able to tamper them (i.e., the forms authentication cookie is encrypted and hashed to prevent tampering), but I could still capture them and store them for a subsequent replay attack.
In the hypothetical banking application above, I initially log in and get SessionIDCookie1 and FormsAuthCookie1. Let’s say the account balance stored in session state corresponding to SessionIDCookie1 is $100.
I don’t log out, but open up another window/tab and somehow prevent (through Fiddler2 maybe) the cookies from being sent through the second window. I log in to that second window.
The web server, noting that the request from the second window has no cookies, starts off another session state, and also returns SessionIDCookie2 and FormsAuthCookie2.
Browsers usually overwrite cookies with the same names, so my SessionCookieID2 and FormsAuthCookie2 are my new session ID and forms authentication cookies.
But remember that I captured SessionIDCookie1 and FormsAuthCookie1 to use in a future attack.
In that second window, I transfer $80 away from my account, thereby updating the session state corresponding to SessionIDCookie2 to be $20. I cannot make another $80 transfer in the second window because I do not have sufficient funds.
Note that SessionIDCookie1 has not been cleaned up and there is a session state on the server corresponding to SessionIDCookie1 which still thinks that the account balance is $100. I now perform my replay attack, sending to the web server SessionIDCookie1 and FormsAuthCookie1. For that given session state, I can make another $80 transfer away from my account.
You might say that the application could easily keep track of the forms authentication cookie issued for a particular user, so that when FormsAuthCookie2 is issued, FormsAuthCookie1 becomes invalid and will be rejected by the server. But what if I use SessionIDCookie1 and FormsAuthCookie2 on the second window? It’s the same result — I can make another $80 transfer away from my account.
Oh, you might say that the application should invalidate SessionIDCookie1 when SessionIDCookie2 is issued. Sure, but how? Unlike the forms authentication cookies, where the user identity is the same within both cookies, there is nothing common between SessionIDCookie1 and SessionIDCookie2. And since there is nothing relating SessionIDCookies with FormsAuthCookies, there’s no mechanism to search for and invalidate SessionIDCookie1.
The only workaround for this is custom code that ties a SessionIDCookie with the FormsAuthCookie that was issued for the same logical session. One of the following options should provide a solution.
- Key your session states by an authenticated user ID instead of by a session ID. No need for the session cookie. This will not work for applications that need to keep track of session without authentication (e.g., online shopping).
- Store the session ID as part of the payload for the forms authentication cookie. Verify that the session ID in the session cookie is the same as that stored in the forms authentication cookie. Keep track of the forms authentication issued for each user so that only a single forms authentication cookie (the most recently issued) is valid for the same user.
Maybe an overarching solution is to avoid storing user-specific information in the session state. Remember that it is a “browser” session state, and has nothing to do with an authenticated user. If you keep that in mind and only store “browser”-related information into session state, then you could avoid the problems altogether.
ASP.NET session fixation is not a very publicized problem, but is potentially a big risk, specially if improper assumptions are made with regard to session and authentication. ASP.NET session fixation is also described long back in http://software-security.sans.org/blog/2009/06/14/session-attacks-and-aspnet-part-1/, and been reported through Microsoft Connect http://connect.microsoft.com/feedback/viewfeedback.aspx?FeedbackID=143361, but to my knowledge, has not been addressed within the ASP.NET framework itself.
ASP.NET Session 清除
// 值为 null,这样对应的 Session 会继续存在,但值为 null
Session["UserId"] = null;
// 移除指定 Session
Session.Remove("UserId");
// 移除所有 Session
Session.Abandon();
ASP.NET WebService 中使用 ASP.NET_SessionId
今天在帮助同事解决对WebService进行 Web References 调用问题:当调用webservice第一个方法Method1发现服务端Set-Cookie: ASP.NET_SessionId=*****保存了ASP.NET的会话状态。接着当我再调用webservice的第二个方法Method2时发现无法正确返回服务器端处理结果
也就是说webservices进行http post 的时无法正常响应服务端。感觉问题就出在cookie上(Method1调用时服务端进行了Set-Cookie),但是就是始终解决不了问题。接着测试webservices浏览器端访问,一切都是正常的。只怪自己对WebService了解甚少,导致明明知道问题所在,就是无法解决问题。。
技术问题往往都是这样,当你越想解决它,它就越难快速的被你搞定。于是乎下班回家后冲凉、吃饱后继续尝试解决问题。我突然想到了long long ago 俺写过一篇文章,就是http抓包的文章,文章地址在这里:http://www.cnblogs.com/ryanding/archive/2011/01/17/1936392.html。主要是通过HttpWebRequest、
HttpWebResponse 这两大神器来解决问题。后来想想这样做肯定可以搞定这个问题。但是还是稍微过于繁琐了。记得
HttpWebRequest 有一个
CookieContainer对象,可以解决cookie问题。于是乎就拼命的在WebServcie的Web References实例化对象上找相关属性。最终完美的被我解决了,代码如下:
ConsoleApplication1.WebReference.YourWebServiceName proxy= new ConsoleApplication1.WebReference.YourWebServiceName();
System.Net.CookieContainer Cookies = new System.Net.CookieContainer();
proxy.CookieContainer = Cookies;
代码执行完以上信息在执行Method1,因为这玩意才开始 Set-Cookie...
接着在执行Method2一切都OK了。
这说明了 proxy.CookieContainer存储了客户端的 ASP.NET_SessionId。这样以后每次通过webservice 方法调用时,都会将ASP.NET_SessionId传递到服务器端。
注:该WEBSERVICES 是客户提供的,我们不能随便修改它的代码。
我想应该也有不少人碰见了类似的问题。本文权当方便后来人吧。
原文链接: http://www.cnblogs.com/ryanding/archive/2011/09/15/2178146.html
关于c# – ASP.NET Session和LINQ和asp.net session用法的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Asp.net Core 源码-SessionExtensions、ASP.NET Session and Forms Authentication and Session Fixation、ASP.NET Session 清除、ASP.NET WebService 中使用 ASP.NET_SessionId等相关知识的信息别忘了在本站进行查找喔。
本文标签: