什么是一个很好的方式来扩展的.Net成员跟踪用户登录 [英] What is a good way to extend .Net Membership to track user logins
问题描述
据我所知,.NET程序会员记录的基本实现lastuserlogin但不跟踪每个登录,这是我现在需要做的。我做了一些研究,我是pretty的确定一种方法是创建一个使用这一功能扩展当前提供的自定义类,并添加事件表的模式来记录每个登录。
As far as I can tell, the basic implementation of .Net membership records lastuserlogin but does not track each login, something I need to do now. I've done some research and I'm pretty sure one approach is to create a custom class that extends the current provider with this functionality, and adding an event table to the schema to record each login.
我已经看到了这几篇文章 - 最接近的可能是这一个从斯科特·米切尔的 http://dotnetslackers.com/articles/aspnet/Tracking-User-Activity.aspx 但它从2008年11月,我很好奇,如果任何人都可以点我朝着一个更好的解决方案,这或证实该仍是'最好'的方式来解决这个问题。我猜想这是一个相当普遍的要求。我仍然在斜坡上。NET(使用C#),所以任何及所有信息将是极大的AP preciated。
I've seen a few articles on this - the closest probably being this one from Scott Mitchell http://dotnetslackers.com/articles/aspnet/Tracking-User-Activity.aspx but it's from Nov 2008 and I'm curious if anyone can point me towards a better solution for this or confirm this is still the 'best' way to tackle this. I would imagine it's a fairly common requirement. I'm still ramping up on .Net (using c#) so any and all information will be greatly appreciated.
修改的
EDIT
进展,但没有完全到那一步。我增加了以下code创建在登录活动表中的一个新的记录:
Progress but not fully there yet. I added the following code to create a new record in the login activity table:
if (Request.IsAuthenticated)
{
MembershipUser currentUser = Membership.GetUser(false);
string App1 = Membership.Provider.ApplicationName;
string username = currentUser.UserName;
object guid1 = currentUser.ProviderUserKey;
aspnet_CustomActivity login = new aspnet_CustomActivity
{
UserId = (Guid) guid1,
ActivityID = 1, //login
DateTime = dt1,
ApplicationName = App1
};
db.aspnet_CustomActivities.InsertOnSubmit(login);
db.SubmitChanges();
}
所以,我有一个小问题,我注意到了应用程序名称是空白的,当我试图在web.config中改变它导致了错误获取currentUser.username但我想我可以工作了这一点。
So I have a small issue where I noticed ApplicationName is blank and when I tried to change it in web.config it caused an error getting the currentUser.username but I think I can work that out.
我现在需要做的到结束,是为了配合这个登录页面,我认为基于下面的提示,我可以做到这一点。我会后再次完成时,让这个答案。
What I need to do now to finish is to tie this to the login page and I think I can do that based on the hints below. I'll post again when complete to round out the answer.
推荐答案
我们简单地创建扩展一个类的的SqlMembershipProvider
,覆盖的ValidateUser
方法来执行任何额外的逻辑,我们想要的。 (你只需要修改<隶属>
你的web.config文件中使用这个新成员提供的部分)。
We simply created a class that extends the SqlMembershipProvider
, overriding the ValidateUser
method to perform whatever extra logic we want. (You just need to change the <membership>
section of your web.config file to use this new membership provider).
修改
还有的实在不多了。创建您的模式一表是指的Guid
基于用户在标准的会员表ID。您可以创建一个类来与此表管理互动。创建您的会员供应商:
There's really not much to it. You create a table in your schema that refers to the Guid
-based user ID in the standard membership tables. You create a class to manage interaction with this table. You create your membership provider:
public class MyMembershipProvider : SqlMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
bool validated = base.ValidateUser(username, password);
if (validated)
{
MembershipUser user = GetUser(username, false); // built in to SqlMembershipProvider
new LoginTracker().RegisterLogin((Guid)user.ProviderUserKey);
}
return validated;
}
}
然后你注册这个提供在web.config中:
Then you register this provider in web.config:
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="60">
<providers>
<clear/>
<add name="MyMembershipProvider" ... type="MyAssembly.MyNamespace.MyMembershipProvider, MyAssembly"/>
</providers>
</membership>
简单!
这篇关于什么是一个很好的方式来扩展的.Net成员跟踪用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!