简体   繁体   中英

How to setup token based authentication in Web API for custom Database Model (Code First) in desktop app

There are plenty of similar questions and I was looking on the web for solution of my particular case but haven't succeeded over past 2 days...

So currently I have 3 separate projects: project with client app that uses HTTPClient to connect with Web API, a project with database model(Code First from database) and project with Web API. I have managed to properly setup Web API so that I can send HTTP requests from my client app and get the data from the server on IIS but can't handle the proper token based authentication.

I tried a few solutions on the web but none of them worked for me or I might not understand sth clearly.

First Solution was: How to customize authentication to my own set of tables in asp.net web api 2? . The idea was to use Identity technology - extend my own User table in my database model by inheriting from IUser. Even without touching anything in Web API project the IUser parent class for my User table caused that my requests stopped working and returned Internal server errors. So I thought - that probably is not the proper way.

Second solution I tried was: DB-First authentication confusion with ASP.NET Web API 2 + EF6 I'm not quite sure what is the userService object in the code in the link above. Seems like it also returns some Identity objects but how does it generate one, does it do all the UserStore objects, UserManager etc inside or just connects to DB context and manually creates some Identity? Even despite those things I don't understand another thing. Becouse in Owin there should be such attribute(for example) above the startup class:

[assembly: OwinStartup(typeof(WebApi2.Startup))]
class Startup
{...}

It lacks in the link above but I guess it's neccessary for Authetication to initialize somehow? With this attribute unfortunately I can't even compile the solution unless project with Web API is checked as Startup Project(in Visual Studio). But in my solution the client app should be the startup project and in such case the following build error shows up: Severity Code Description Project File Line

Error       Unknown build error, 'Cannot resolve dependency to assembly 'Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.'    BooksLibraryV2      

I am already totally confused with this authentication... If someone could point me into the right direction eventually I would be really grateful. I will grant the bounty as well if anybody's willing to clarify me the right approach.

Syntax - Code first approach to create database from application - for OWin Identity Models

1) enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
2) Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
3)Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose

Example

Enable-Migrations -ContextTypeName:OwinAuthenticationWebApiApplication.Models.OwinAuthDbContext -MigrationsDirectory:EntitiesMigrations

Add-Migration –configuration OwinAuthenticationWebApiApplication.EntitiesMigrations.Configuration InitialEntities

Update-Database -configuration:OwinAuthenticationWebApiApplication.EntitiesMigrations.Configuration -Verbose

Turned out that the attribute:

[assembly: OwinStartup(typeof(WebApi2.Startup))]

is not neccessary, though it's by default in Web API Individual Users Authentication template... OWIN automatically searches all assemblies just for a class called Startup. So after deleting this attribute I finally could compile the solution and setup basic token authentication following tips mentioned by Fabio Luz DB-First authentication confusion with ASP.NET Web API 2 + EF6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM