Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迁移身份为2.x的MVC5中的匿名用户_C#_Asp.net_.net_Asp.net Mvc_Asp.net Identity - Fatal编程技术网

C# 迁移身份为2.x的MVC5中的匿名用户

C# 迁移身份为2.x的MVC5中的匿名用户,c#,asp.net,.net,asp.net-mvc,asp.net-identity,C#,Asp.net,.net,Asp.net Mvc,Asp.net Identity,我的问题与这个问题同时出现(但是,除非你阅读所有评论和答案,否则你可能无法理解): 我只是想解释一下,或者问得更清楚一点。我的问题也是关于这篇关于迁移匿名用户的Microsoft文章: 我想最简单的问题是,MSDN文章对于迁移MVC5 Identity 2.X中的匿名用户是否仍然是最新的/相关的?首先,它提到: <authentication mode="Forms" > <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH

我的问题与这个问题同时出现(但是,除非你阅读所有评论和答案,否则你可能无法理解):

我只是想解释一下,或者问得更清楚一点。我的问题也是关于这篇关于迁移匿名用户的Microsoft文章:

我想最简单的问题是,MSDN文章对于迁移MVC5 Identity 2.X中的匿名用户是否仍然是最新的/相关的?首先,它提到:

<authentication mode="Forms" >
  <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" />
</authentication>

正如您所看到的,在msdn示例中,只有一行似乎与标识相关(……而且通常,cookie似乎也与标识无关。)事实是,我只得到了少数几个有关
Profile\u OnMigrateAnonymous
和其他与此相关的项目的搜索结果,包括国际结果,让我觉得这不是一种流行的做事方式?似乎SimpleMembership也让匿名用户想得更透彻一些?事实上,匿名用户似乎根本不被认为具有身份。

嗯。。。这是我在不杀人的情况下所能做的最好的事情,谁知道还有多少天。我不会把它标记为被接受的答案,因为我真的觉得它不是。为了在我死之前完成这个项目(因为.NET每天都在使我老化1年),我不得不采用尼安德特人的方法,逐步了解它对我的意义

我做的第一件事是在我的表上创建一个唯一的约束(同一个sql表中相同的两个数据段没有重复项,即IncidentId和IUserId),如下所示:

    CREATE UNIQUE NONCLUSTERED INDEX idx_incidentid_iuserid
    ON Likes(IncidentId, IUserId)
在my global.asax中(还没有):

我认为为这个约束抛出一个异常可能不是最需要性能良心的事情,但是我不知道还有什么其他的方法来实现它。对于该用户,每个IncidentId最多只能有一条重复记录,因此它似乎适合居住。然而,我想知道一个更好的方法

希望这能帮助像我这样的可怜的迷失的灵魂。我很想听到任何其他的评论,陷阱,建议等等。谢谢

<anonymousIdentification enabled="true" />
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);

    }
    CREATE UNIQUE NONCLUSTERED INDEX idx_incidentid_iuserid
    ON Likes(IncidentId, IUserId)
    ...
    using System.Web.Security;
    using Microsoft.AspNet.Identity;
    using ProjectSender.Models;
    ...
    ...
        public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            var anonymousUser = args.AnonymousID;
            var identityUser = User.Identity.Name;
            var identityUserId = User.Identity.GetUserId();

            foreach (var item in db.Likes.Where(x => x.IUserId == anonymousUser).ToList())
            {
                //Try = Update anonymousId with identityUserId. 
                //Catch = Remove any duplicates caught by the exception/constraint
                try
                {
                    item.IUserId = identityUserId;
                    item.IUser = identityUser;
                    db.SaveChanges();
                }
                catch
                {
                    db.Likes.Remove(item);
                    db.SaveChanges();
                }
            }

            // Remove the anonymous cookie.
            AnonymousIdentificationModule.ClearAnonymousIdentifier();
        }
    ...