|
using Microsoft.AspNet.Identity.EntityFramework; |
|
using Microsoft.Owin.Security.OAuth; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Security.Claims; |
|
using System.Threading.Tasks; |
|
using System.Web; |
|
|
|
namespace AngularJSAuthentication.API.Providers |
|
{ |
|
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider |
|
{ |
|
/// <summary> |
|
/// Validating the “Client”, in our case we have only one client so we’ll always return that its validated successfully. |
|
/// </summary> |
|
/// <param name="context"></param> |
|
/// <returns></returns> |
|
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) |
|
{ |
|
context.Validated(); |
|
} |
|
|
|
/// <summary> |
|
/// Responsible to validate the username and password sent to the authorization server’s token endpoint |
|
/// If the credentials are valid we’ll create “ClaimsIdentity” class and pass the authentication type to it, |
|
/// in our case “bearer token”, then we’ll add two claims (“sub”,”role”) and those will be included in the signed token. |
|
/// |
|
/// </summary> |
|
/// <param name="context"></param> |
|
/// <returns></returns> |
|
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) |
|
{ |
|
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); |
|
using (AuthRepository _repo = new AuthRepository()) |
|
{ |
|
IdentityUser user = await _repo.FindUser(context.UserName, context.Password); |
|
if (user == null) |
|
{ |
|
context.SetError("invalid_grant", "The user name of password is incorrect."); |
|
return; |
|
} |
|
} |
|
|
|
var identity = new ClaimsIdentity(context.Options.AuthenticationType); |
|
identity.AddClaim(new Claim("sub", context.UserName)); |
|
identity.AddClaim(new Claim("role", "user")); |
|
|
|
context.Validated(identity); |
|
} |
|
} |
|
} |