If you are using ASP.NET Membership Provider for a website, one simple and practical way how to get ‘username’ for logging activities on CRUD (Create Read Update Delete) implementation is by storing the “username” on a session. Put this little code on Default page, or first landing page after succesfull user login.
1 |
using System.Web.Security; |
…
1 2 3 4 5 6 7 8 9 |
protected void Page_Load(object sender, EventArgs e) { //check user login if (User.Identity.IsAuthenticated) { //get username and put into session Session["UserName"] = Membership.GetUser().UserName.ToString(); } } |
Next example, if you are using DataSet , just put the session name on a parameter for later use on SQL Command.
1 2 3 |
<InsertParameters> <asp:SessionParameter DefaultValue="na" Name="UserName" SessionField="UserName" Type="String" /> <InsertParameters> |