Guest User

Untitled

a guest
Jul 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. using EventTracker.Models;
  2. using Microsoft.Owin.Security.OAuth;
  3. using System.Security.Claims;
  4. using System.Threading.Tasks;
  5. using System.Linq;
  6.  
  7. namespace EventTracker.Helpers {
  8. public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider {
  9. public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) {
  10. // OAuth2 supports the notion of client authentication
  11. // this is not used here
  12. context.Validated();
  13. }
  14.  
  15. public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) {
  16. // validate user credentials (demo!)
  17. // user credentials should be stored securely (salted, iterated, hashed yada)
  18. var db = new AppContext();
  19. var hashedPassword = context.Password.ToHash();
  20. var user = db.Users.FirstOrDefault(u => (u.Email == context.UserName || u.Name == context.UserName) && u.Password == hashedPassword);
  21. if (user == null) {
  22. context.Rejected();
  23. return;
  24. }
  25.  
  26. // create identity
  27. var id = new ClaimsIdentity(context.Options.AuthenticationType);
  28. id.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
  29. user.UserRoles.ToList().ForEach(ur=>{
  30. id.AddClaim(new Claim(ClaimTypes.Role, ur.RoleId));
  31. });
  32.  
  33. context.Validated(id);
  34. }
  35. }
  36. }
Add Comment
Please, Sign In to add comment