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.
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
Hi Mark. Could you please try to post in English. It’s not understandable in this form ;-).
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.
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
Hi Jan. I think you are trying to use Google. They changed their access so now you have to provide a Client Id. Thats it. Just check the provider you use in AuthConfig.
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
Hard to answer. If you can, send me some sample-code to sprinter_at_codingfreaks.de.
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
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.
Thanks so much, Alex!
I’ll try this now.
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.
Hi Alexander,
I have the same problem as Illia.. any sugestions?
BR
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.
To all of you guys which had problems with the solution. I’ll try to find time to build up a fresh solution based on MVC 5 soon. Please stand by.
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
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
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
Thanks so much for the video. It really helped!
However I am having a problem with an extra email column that I didn’t specify. I posted it on Stack Overflow. Maybe someone here can take a look?
http://stackoverflow.com/questions/31322922/identity-2-0-adding-extra-email-column
Thanks Sprinter, i have struggled with identity until i stumbled upon your video. thanks a million.
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.
Hi Andrew. No, not yet. I just can’t find enough time to make this post happen. I try my best.
Thank you. Please do.
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
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?
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).
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.
I am also having a similar problem with login. I am able to register the user with hashed password using SHA1 as shown in the tutorial. The user is stored in the database with the password hash. However, when I logout and try to login again, the line below in the Login method always returns an error:
var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false);
the error says: the input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Please help.
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.
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.