-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rushi216
committed
Feb 22, 2016
1 parent
72e4068
commit 6c9bea8
Showing
9 changed files
with
190 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using MvcStarter.Identity; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Web.Http; | ||
|
||
namespace MvcStarter.Controllers | ||
{ | ||
[Authorize] | ||
public class SecureController : ApiController | ||
{ | ||
private readonly ApplicationUserManager _userManger; | ||
|
||
public SecureController(ApplicationUserManager userManager) | ||
{ | ||
_userManger = userManager; | ||
} | ||
|
||
|
||
[HttpGet] | ||
[Route("api/me")] | ||
public IHttpActionResult Me() | ||
{ | ||
return Ok(true); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNet.Identity; | ||
using Microsoft.AspNet.Identity.EntityFramework; | ||
using Microsoft.Owin; | ||
using Microsoft.Owin.Security.Cookies; | ||
using Microsoft.Owin.Security.Google; | ||
using Microsoft.Owin.Security.OAuth; | ||
using Owin; | ||
using System.Threading.Tasks; | ||
using Microsoft.Owin.Security; | ||
using System.Security.Claims; | ||
using Microsoft.AspNet.Identity.Owin; | ||
|
||
namespace MvcStarter.Identity | ||
{ | ||
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider | ||
{ | ||
private readonly ApplicationUserManager _userManager; | ||
|
||
public ApplicationOAuthProvider(ApplicationUserManager userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) | ||
{ | ||
|
||
ApplicationUser user = await _userManager.FindAsync(context.UserName, context.Password); | ||
|
||
if (user == null) | ||
{ | ||
context.SetError("invalid_grant", "The user name or password is incorrect."); | ||
return; | ||
} | ||
|
||
ClaimsIdentity oAuthIdentity = await _userManager.CreateIdentityAsync(user, OAuthDefaults.AuthenticationType); | ||
ClaimsIdentity cookiesIdentity = await _userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); | ||
|
||
AuthenticationProperties properties = CreateProperties(user.UserName); | ||
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); | ||
context.Validated(ticket); | ||
context.Request.Context.Authentication.SignIn(cookiesIdentity); | ||
} | ||
|
||
public override Task TokenEndpoint(OAuthTokenEndpointContext context) | ||
{ | ||
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary) | ||
{ | ||
context.AdditionalResponseParameters.Add(property.Key, property.Value); | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) | ||
{ | ||
// Resource owner password credentials does not provide a client ID. | ||
if (context.ClientId == null) | ||
{ | ||
context.Validated(); | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context) | ||
{ | ||
if (context.ClientId == "self") | ||
{ | ||
Uri expectedRootUri = new Uri(context.Request.Uri, "/"); | ||
|
||
if (expectedRootUri.AbsoluteUri == context.RedirectUri) | ||
{ | ||
context.Validated(); | ||
} | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public static AuthenticationProperties CreateProperties(string userName) | ||
{ | ||
IDictionary<string, string> data = new Dictionary<string, string> | ||
{ | ||
{ "userName", userName } | ||
}; | ||
return new AuthenticationProperties(data); | ||
} | ||
} | ||
} |
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
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
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
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