Microsoft.AspNet.Identity in bestehender Anwendung einsetzen Teil 2

Ich hatte bereits einen Beitrag zum diesem Thema gepostet. Dieser bezog sich allerdings noch auf die Prerelease-Version von ASP.NET Identity 2. Jetzt endlich konnte ich das Update dafür fertig machen.

Follow-Up zu: Microsoft.AspNet.Identity in bestehender Anwendung einsetzen

Source inkl. SQL-Scripts: Download im ZIP-Format

Screencast

Dieses Mal werde ich nicht mit einem eigenen Beitrag auf das Thema eingehen (Zeitgründe). Das Video in englisch sollte erstmal genügen.

29 Antworten auf „Microsoft.AspNet.Identity in bestehender Anwendung einsetzen Teil 2“

  1. hallo herr Schmidt,
    Ich habe eine Frage? Wie können Sie dies Webforms Anwendung. Ich bekomme Fehlermeldungen, weil ständig und ich kann nicht starten / das Projekt.

    Mit freundlichen Grüßen,

    Mark Kooijman

  2. Hi Sprinter. Danke vielmals für Deinen Artikel und die Beispielprojekt. Das hat mir echt weitergeholfen :-)

    Grüsse
    Daniel

    PS: Übrigens, nach dem Entpacken des Source Zip-Files enthalten die Projekte IdentitySample und IdentityDefault anstelle eines App_Data-Folders eine App_Data-Datei. Vielleicht war da das Problem von Mark. Aber sobald man das korrigiert funktionierts.

  3. Hi,
    I tried implementing your solution into my and I got this error „The ‚ClientId‘ option must be provided.“.
    I don’t know why?

    br,
    Jan

  4. Hi,
    I try to apply the code you show in my project
    However when i register a new account it show an exception like:

    „The entity type MyUser is not part of the model for the current context.“

    at Source File: c:\Users\RichardFk\Documents\Visual Studio 2013\Projects\Homies\Homies\Controllers\AccountController.cs Line: 340

    Where do you think it goes wrong

  5. Hi Alex –

    Thank you so much for this informative blog post!

    I downloaded your project, ran the sql script and went through the youtube video. The IdentityDefault project worked fine – but I received the same error as Jan above when I tried to run IdentitySample: The ‚ClientId‘ option must be provided.

    I have not changed anything in your code – other than updating the connectionString.

    Any wisdom?

    All the best,

    – Rich

    1. Hi Rich. This is because ClientId is a non-nullable field in the database. So one solution could be to set it to a Guid for instance before you create a new user. Another option would be to set ClientId nullable in database.

  6. Hi Alexander,

    Thanks for your video, it is very helpful. I’ve just modified my solution according to your explanation and it works perfectly. (Additional thanks for the script.)

    But I had to comment a part of app.UseCookieAuthentication(…) call in App_Start/Startup.Auth.cs. Following part raised error on startup:

    Provider = new CookieAuthenticationProvider
    {
    OnValidateIdentity =
    SecurityStampValidator.OnValidateIdentity(
    TimeSpan.FromMinutes(30),
    (manager, user) => user.GenerateUserIdentityAsync(manager),
    identity => long.Parse(identity.GetUserId()))
    }

    Attempt to cast identity.GetUserId() to long failed since it was returning a GUID. And I don’t know why. Why GUID? When I commented this call (whole Provider property assignment) identity.GetUserId() in AccountController always returns long value as it suppose to return.

    My first thought was that default ASP.Net Identity was somehow involved (I’ve tried default identity in my solution and I’ve already added one record to AspNetUser table – with GUID Id). But same error remained even after I deleted .mdf file from App_Data folder. I’ve also could messed up with connection strings (I decided to use LocalDB during development).

    Any way, I’m good with your solution even with commented Provider assignment (I don’t understand its significance for now). But it would be great if you give me a hint what can cause the issue and what consequences I could face with commented Provider.OnValidateIdentity() assignment.

    Thanks again,
    Illia.

  7. Hi Alexander,

    I am not able to load the project from source code like in Visual Studio Express 2013,

    IdentityDefault.csproj : error : The item ‚App_Data‘ does not exist in the project directory. It may have been moved, renamed or deleted.

    IdentitySample.csproj : error : The item ‚App_Data‘ does not exist in the project directory. It may have been moved, renamed or deleted.

    Please suggest how to resolve this error.

  8. Hi,

    First of all thanks for the lessons and tutorials. I wanted to ask is it possible to work with same DB context used by EDMX having different field names from Identity 2.0.

    Identity Naming Convention:
    Id, UserName, PasswordHash, SecurityStamp

    My Database Fields:
    USER_ID, USER_NAME, PASSWORD_HASH, SECURITY_STAMP

    Regards,
    Faizan Haq

  9. Dear sprinter,

    First of all thanks for the video. It helps a lot.

    But I got a problem while I build the sample project.

    error msg:
    The entity type MyUser is not part of the model for the current context.

    error comes:
    AccountController.cs Register()
    var result = await UserManager.CreateAsync(user, model.Password);

    Please help.

    Best regards,
    David

    1. for those who are getting error about

      The entity type is not part of the model for the current context

      solution is to change the connection string saved by entityframework in webconfig . change that connection string to an SQL connection string

      i’ve done this and my project is now working

  10. Hi Sprinter,

    Thank you for this screen cast. Do you have an updated version of the source code since asp.net identity code has changed somewhat after the making of this video.

  11. Thanks a lot. Wonderful video. A small question – If I have existing users in my User table, do I’ve to prepopulate other tables (Role, Claim, etc.). I cannot login using my existing user’s UserName and Password. Any help will be really appreciated.
    Thanks
    Kish

  12. Thank you so much for the wonderful cast.

    I’m trying to implement a scenario with aspnet identity and I can’t figure out how. I want user to be able to provide the environment at the login time. Basically, when user logs in, I would like him to select a string (Environment drop down) as well as credential. Later I would like to choose my IdentityDbContext connection string based on that value. Preferably, I’d like to have it in the claim set.

    Any guidance?

    1. Probably you can do that. You have to connect the user selection with connections-string-keys. When the user selects one area you can create your Entity Conetext by using the constructor that accepts the connection-string-key from the config. Something like var ctx = new IdentityContext(connectionStringNameFromConfig).

  13. Thanks for this beautiful presentation,it help me a lot,But i tried to implement the Identity 2.0 by watching your video part-2, It work for registration part .But when i tried to Login its shows me „Invalid username and password“, I am unable to login by using proper password.
    could you please guide me what might the error.

  14. hello i used user identity.models project and added it to my project. i have a custom db where i have userid field as custom field. i want the asp identity aspnetusers table mapped to this table with column name userID. i did the code change and the changes are below,
    Myuser.cs

    public class MyUser : IdentityUser
    {
    #region properties
    public Nullable LCODetailsID { get; set; }
    public Nullable EmpDetailsID { get; set; }
    public Nullable AreaID { get; set; }
    // sprinter: I cutted them out because they don’t make it clearer
    #end region
    }

    applicationdbcontext.cs
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    base.OnModelCreating(modelBuilder);
    // Map Entities to their tables.
    modelBuilder.Entity().ToTable(„Users“).Property(p => p.Id).HasColumnName(„UserId“); ;
    modelBuilder.Entity().ToTable(„RoleIdentity“);
    modelBuilder.Entity().ToTable(„UserClaim“);
    modelBuilder.Entity().ToTable(„UserLogin“);
    modelBuilder.Entity().ToTable(„UserRoleIdentity“);
    // Override some column mappings that do not match our default
    }

    i have mapped myuser class to my existing table users in modelcreating and named the id column to be userid. but that doesnt seem to be working. I get a DbEntityValidationException with a message that id field is required. can you help me on this.

    1. Hello Boopal Subramaniam! I just edited your question to make it more readable and not so long. However first of all you are using EF with code-first which is not what I’ve showed in the tutorial. This changes a lot of stuff. Anyway. I cannot see, where your „UserId“-property should come from and you don’t take care about the original id-field at all as far as I can see from your sample. My tutorial is not about just inheriting from IdentityUser but instead replacing it with an own implementation. So it might be better for you to post your problem on stackoverflow I guess.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden .