yellephen
yellephen

Reputation: 129

Browse Database EF ASP.Net Core MVC

I'm playing around with a web application using the above and visual studio 2016 community. I've pretty much still got the default project using Individual User Account as the authentication. So it's built all the views and models around authentication and I can register accounts and log in. I'm now wanting to add some attributes to the default and running into some unknowns.

First question is about writing some non-default attributes. I've looked into how it's writing the default attributes (email and phone) and there are actually methods in the UserManager library class to update these attributes

public virtual Task<IdentityResult> SetEmailAsync(TUser user, string email);
and
public virtual Task<IdentityResult> SetPhoneNumberAsync(TUser user, string phoneNumber);

Obviously I can't add methods for my own attributes like SetTagLineAsync... because this is a library class. I found it a little strange that they have such specific methods in a library but anyway I have the user object in context in my controller (which you can see above in the virtual methods) and I think I should be able to just update that object and call this method

public virtual Task<IdentityResult> UpdateAsync(TUser user);

And it'll update the user in the database to use the attributes I've set on the user. But this leads into my second question, because I'm not certain how this works yet I would want to try the above method and then go check in the database if it did update the attribute I wanted.

Second question: How do I view the projects database? There is obviously a database that was created with the project template and it is persistent because if I run the web app and register, when I run it again I can log in as that user. When I make a change to the ApplicationUser class I also have to add a migration and update the database through package manager console. I can see in my app settings

"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MyApp-3B07911E-A6DD-4B3E-A533-57B04E37B791;Trusted_Connection=True;MultipleActiveResultSets=true"

I don't have SQL server installed if that matters, just whatever sql stuff was installed with VS. In other projects i just go to Server Explorer and my database appears under Data Connections. I've tried to add the data connection but when I do, I add the server name (localdb) – i also tried without the brackets – and then when I try to select a database name it throws connection errors. I've also tried doing this while the webapp is open by disconnecting the debugger and trying the same process in case it only exists while it's running but same thing. Any ideas?

Upvotes: 0

Views: 90

Answers (1)

sramekpete
sramekpete

Reputation: 3208

Obviously I can't add methods for my own attributes like SetTagLineAsync...

You can add your own method(s) inheriting from UserManager class.

UpdateAsync vs. SetXXXAsync methods

SetXXXAsync methods are more complex and have additional logic inside. For example SetEmailAsync and SetPhoneAsync are not only setting Email and Phone property/column, but also EmailConfirmed = false and PhoneConfirmed = false. Both are also setting new security stamp that is used to indicate, if user changed profile. In that case he/she needs to perform new login on (other) devices(rejecting cookies that was set before the change happened). Which is useful security feature. It will be really bad if you change your password, but cookies set with old one will still work.

    public virtual async Task<IdentityResult> SetPhoneNumberAsync(TUser user, string phoneNumber)
    {
        ThrowIfDisposed();
        var store = GetPhoneNumberStore();
        if (user == null)
        {
            throw new ArgumentNullException(nameof(user));
        }

        await store.SetPhoneNumberAsync(user, phoneNumber, CancellationToken);
        await store.SetPhoneNumberConfirmedAsync(user, false, CancellationToken);
        await UpdateSecurityStampInternal(user);
        return await UpdateUserAsync(user);
    }

How do I view the projects database?

You have to use (localdb)\MSSQLLocalDB as server name.

LocalDb is development database and may be installed during Visual Studio installation.

Related links:

Introduction to Identity on ASP.NET Core

UserManager class (source code)

What is ASP.NET Identity's IUserSecurityStampStore interface?

How to connect to LocalDB in Visual Studio Server Explorer?

Introducing LocalDB, an improved SQL Express

Upvotes: 1

Related Questions