简体   繁体   中英

C# WebAPI Authentication

I'm pretty new to building a web api so please forgive me if anything in this question is absurdly stupid. My main goals it so make an web api interface between clients and an already existing database. Currently I've managed to build something which can interact with an SQL database using C# and ASP.net. The issue I have currently is that I'm struggling with authentication. I've spent about 3/4 days looking over this topic trying to understand it but unfortunately I've been unsuccessful thus far.


Currently I believe the issue I am having at the moment is that whenever I try to register an account using the API. Below is the register func within the ApplicationController

(Its what was auto generated when I created the project through Visual Studio 2015)

// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

    IdentityResult result = await UserManager.CreateAsync(user, model.Password);

    if (!result.Succeeded)
    {
        return GetErrorResult(result);
    }

    return Ok();
}

The model state is fine but this:

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

always returns false.

I thought it was due to some issue with the database connection to the membership database. After looking into that I found I had no such database within my project or on my localhost sql server. Found out online how to create a membership database (using the regsql.exe). So, I created the membership database, put it onto my sql2008rs localhost server and change the connection string within the project's web.config file

<add name="DefaultConnection" 
connectionString="Data Source=localhost\sql2008r2;Initial Catalog=SecurityDatabase;Integrated Security=True;" 
providerName="System.Data.SqlClient" />

Thinking this was all I'd need to do to get it to work I try and test it, still not working. Then using the Sql Server profiler too in SQL SMS I run a trace to see what SQL is sent

exec sp_executesql N'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@Table',N'@Table nvarchar(11)',@Table=N'AspNetUsers'

Is what was sent which confuses me on a number of levels. Firstly why is it sending a select statement when I'm trying to register a new user and secondly why is it trying to select from a table that doesn't exist? There is no AspNetUser but there is a Aspnet_user table.


I'm assuming somewhere along the line I've gone terribly wrong and being new to all of this I'm sure I've made some very dumb mistakes but I've spent literal days looking for information and help and all I can find is vague stuff about MVC or Webforms which seem to just say that the membership database is created by magic when you run the application.

Below is a screenshot of the object explorer in SQL SMS showing the existing database and its tables http://i.imgur.com/cQCaHRP.png

I'm dumb. Thank you to Sam Farajpour for pointing out my sheer stupidity of not realising that result has other properties, the error I had was to do with some password requirements I wasn't aware of and have now fixed my issue.

What did I learn from this? Check everything once, twice and then three times fully.

I doubt anyone else will have the same issue as myself but if anyone does, check the result object and look at its properties. There is a string array which contains the error information if there is any. You can figure it out from there easy.

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