【发布时间】:2015-06-02 10:54:58
【问题描述】:
我的问题与这个问题密切相关(但是,除非您阅读了那里的所有 cmets 和答案,否则您可能无法弄清楚):
Does ASP.NET Identity 2 support anonymous users?
我只是想详细说明或问得更清楚一点。我的问题也与这篇关于迁移匿名用户的 Microsoft 文章有关:
我想最简单的问题是,MSDN 文章是否仍然适用于在 MVC5 Identity 2.X 中迁移匿名用户?一方面,它提到了这一点:
<authentication mode="Forms" >
<forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" />
</authentication>
但是,在我的 web.config 中,我有这个(它是由我以外的人添加的......我假设身份或项目模板。):
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
我也在我的 web.config 中使用它,它似乎可以很好地处理匿名用户(它是我添加的,在 MSDN 文章中提到):
<anonymousIdentification enabled="true" />
从匿名到IsAuthenticated有两种方式,注册或登录。
基本上,无论匿名用户被允许做什么,我都会抓取 AnonymousID 并将其放入包含相关信息的单独表中(例如 CommentId => AnonymousId)。 (从理论上讲,用户可能已经注册并注销并且仍然充当匿名用户......本质上是重复操作,当用户登录时再次迁移数据时需要考虑这一点,因此它不会创建重复.)
我引用的这两个链接以及可能涉及该主题的其他几篇文章,但没有什么是真正清楚或很好解释的。关于第一段代码,是否有新的authentication mode?假设我们还没有使用Forms,我错了吗? MSDN 示例仍然是最新的吗?将匿名用户与匿名用户可能链接到的任何其他表数据一起迁移到 IsAuthenticated 的更好示例(如果可能)或更当前的方法是什么?
谢谢。
更新 1(次日):这是我目前所拥有的:
Global.asax 中的Profile_OnMigrateAnonymous 仍会在成功注册和登录事件时触发。示例中添加了注释:
public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
{
//Anything regarding this profile stuff appears to not be relevant to Identity...
//... (and no using statements I can find will get rid of the squigglies...
//... Even if I get rid of the Profile error, GetProfile has problems.)
//ProfileCommon anonymousProfile = Profile.GetProfile(args.AnonymousID);
//More Profile stuff and not even relevant to what I'm doing...
//...and appears to be new fields you can add to the SimpleMembership Users table?
//...and also related to the properties you add in your web.config which are also...
//...not related to Identity.
//Profile.ZipCode = anonymousProfile.ZipCode;
//Profile.CityAndState = anonymousProfile.CityAndState;
//Profile.StockSymbols = anonymousProfile.StockSymbols;
////////
//// Delete the anonymous profile. If the anonymous ID is not
//// needed in the rest of the site, remove the anonymous cookie.
//The ProfileManager line just hangs and barks about a network error...
//...but there is no network error (without a doubt)...
//...I have no idea what it would be deleting anyway.
//ProfileManager.DeleteProfile(args.AnonymousID);
//This is the only line that is successful and actually deletes the cookie.
AnonymousIdentificationModule.ClearAnonymousIdentifier();
//// Delete the user row that was created for the anonymous user.
// Except that no user was written to any user rows...
// ...Therefore, I didn't even bother trying this next line.
//Membership.DeleteUser(args.AnonymousID, true);
}
如您所见,在带有 Identity 的 msdn 示例中似乎只有一行有效(...而且通常,cookie 似乎也与 Identity 无关。)事实上我只得到了少数Profile_OnMigrateAnonymous 的搜索结果以及与此相关的其他项目(包括国际搜索结果)让我觉得这不是一种流行的做事方式?似乎 SimpleMembership 可能已经让匿名用户考虑得更彻底一些?实际上,Identity 似乎根本没有考虑匿名用户。
【问题讨论】:
标签: c# asp.net .net asp.net-mvc asp.net-identity