Created
April 20, 2018 10:50
-
-
Save foyzulkarim/7d27fc76045cdda60b2db18d1890df4d to your computer and use it in GitHub Desktop.
Azure Function is used here as ASP.NET Identity Token provider for SPA Applications. Then this token will be used for other secured API calls, eg. saving a Sale entity to database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
namespace FunctionApp1 | |
{ | |
using System; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Claims; | |
using System.Text; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.AspNet.Identity.EntityFramework; | |
using Microsoft.IdentityModel.Tokens; | |
using Newtonsoft.Json; | |
using Server.Identity; | |
using Server.Identity.Models; | |
public class SigninModel | |
{ | |
public string UserName { get; set; } | |
public string Password { get; set; } | |
} | |
public static class SigninFunction | |
{ | |
[FunctionName("SigninFunction")] | |
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
var readAsStringAsync = await req.Content.ReadAsStringAsync(); | |
var signinModel = JsonConvert.DeserializeObject<SigninModel>(readAsStringAsync); | |
var dbContext = new SecurityDbContext(); | |
var store = new UserStore<ApplicationUser>(dbContext); | |
var userManager = new ApplicationUserManager(store); | |
ApplicationUser user = await userManager.FindAsync(signinModel.UserName, signinModel.Password); | |
string token = BuildToken(user); | |
return req.CreateResponse(HttpStatusCode.OK, token); | |
} | |
private static string BuildToken(ApplicationUser user) | |
{ | |
var claims = new[] { | |
new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName), | |
new Claim(JwtRegisteredClaimNames.Email, user.Email), | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
}; | |
string bizbook365 = "http://bizbook365.com"; | |
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(bizbook365)); | |
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); | |
var jwtSecurityToken = new JwtSecurityToken( | |
bizbook365, | |
bizbook365, | |
claims, | |
expires: DateTime.Now.AddMinutes(30), | |
signingCredentials: creds); | |
JwtSecurityTokenHandler securityTokenHandler = new JwtSecurityTokenHandler(); | |
string token = securityTokenHandler.WriteToken(jwtSecurityToken); | |
return token; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
namespace FunctionApp1 | |
{ | |
using System.Net.Http.Headers; | |
using System.Security.Claims; | |
using CommonLibrary.Repository; | |
using Model; | |
using Model.Sales; | |
using ServiceLibrary.Sales; | |
public static class SaleCommands | |
{ | |
[FunctionName("SaleAdd")] | |
public static async Task<HttpResponseMessage> RunAdd([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
#region Auth region | |
AuthenticationHeaderValue authorization = req.Headers.Authorization; | |
if (authorization == null) | |
{ | |
return req.CreateErrorResponse(HttpStatusCode.Unauthorized, "Provide Authorization header value"); | |
} | |
ClaimsPrincipal principal; | |
if ((principal = await ApplicationSecurityService.ValidateTokenAsync(authorization).ConfigureAwait(true)) == null) | |
{ | |
return req.CreateResponse(HttpStatusCode.Unauthorized); | |
} | |
string userName = principal.Identity.Name; | |
var user = ApplicationSecurityService.GetApplicationUser(userName); | |
#endregion | |
// Get request body | |
Sale data = await req.Content.ReadAsAsync<Sale>(); | |
SaleService saleService=new SaleService(new BaseRepository<Sale>(BusinessDbContext.Create())); | |
data = ApplicationInventoryService.EntitySavePreparation(user, data) as Sale; | |
bool add = saleService.Add(data); | |
if (add) | |
{ | |
return req.CreateResponse(HttpStatusCode.OK, new { data.Id, data.OrderNumber }); | |
} | |
else | |
{ | |
return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid data"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment