Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Web API Post Method

$
0
0

Hi,

Am sorry to have ask for this question but i have search for resource with any help, I need to convert this part of the code to the pattern of Repository and Controller method but without any solution, please can anyone point me to resources or solution to help fixing this.

The code below is a sample tutorial by Taiseer Joudeh, Which he persist the information into a volatile dictionary so the values for those Audiences will be removed from the memory if IIS reset toke place

But i want to use it in production serve which i want to store those values permanently on a database. I want to know how the the repoistory format and the controller format will be.

The below code was from this resources: http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

public static class AudiencesStore
{
public static ConcurrentDictionary<string, Audience> AudiencesList = new ConcurrentDictionary<string, Audience>();

static AudiencesStore()
{
AudiencesList.TryAdd("099153c2625149bc8ecb3e85e03f0022",
new Audience { ClientId = "099153c2625149bc8ecb3e85e03f0022",
Base64Secret = "IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw",
Name = "ResourceServer.Api 1" });
}

public static Audience AddAudience(string name)
{
var clientId = Guid.NewGuid().ToString("N");

var key = new byte[32];
RNGCryptoServiceProvider.Create().GetBytes(key);
var base64Secret = TextEncodings.Base64Url.Encode(key);

Audience newAudience = new Audience { ClientId = clientId, Base64Secret = base64Secret, Name = name };
AudiencesList.TryAdd(clientId, newAudience);
return newAudience;
}

public static Audience FindAudience(string clientId)
{
Audience audience = null;
if (AudiencesList.TryGetValue(clientId, out audience))
{
return audience;
}
return null;
}
}

And this Controller


[RoutePrefix("api/audience")]
public class AudienceController : ApiController
{
[Route("")]
public IHttpActionResult Post(AudienceModel audienceModel)
{
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}

Audience newAudience = AudiencesStore.AddAudience(audienceModel.Name);

return Ok<Audience>(newAudience);

}
}


Am new to web api, and your blog is my first resources in Web API,

Thanks


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>