Created
April 11, 2012 09:58
-
-
Save mgroves/2358332 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Web.Mvc; | |
| using AopBlog.Models.Repositories; | |
| using AopBlog.Models.Services; | |
| namespace AopBlog.Areas.Manage.Controllers | |
| { | |
| public class AccountController : Controller | |
| { | |
| readonly IAuthorizationService _authService; | |
| readonly IAuthorRepository _authorRepo; | |
| public AccountController(IAuthorRepository authorRepoRepository, IAuthorizationService authService) | |
| { | |
| _authorRepo = authorRepoRepository; | |
| _authService = authService; | |
| } | |
| public ViewResult LogOn() | |
| { | |
| return View(); | |
| } | |
| public void TwitterOAuth() | |
| { | |
| _authService.RequestOAuthAuthentication(); | |
| } | |
| public RedirectToRouteResult OAuth() | |
| { | |
| var response = _authService.ProcessAuthentication(); | |
| if (response.IsAuthenticated) | |
| { | |
| if (_authorRepo.AuthorIsAuthorized(response.ScreenName)) | |
| { | |
| _authService.CreateAuthCookie(response.ScreenName, response.AccessToken); | |
| return RedirectToAction("Index", "Home"); | |
| } | |
| return RedirectToAction("Unauthorized", new {response.ScreenName}); | |
| } | |
| return RedirectToAction("LogOn"); | |
| } | |
| public ViewResult Unauthorized(string screenName) | |
| { | |
| ViewBag.ScreenName = screenName; | |
| return View(); | |
| } | |
| } | |
| } |
This file contains hidden or 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; | |
| using System.Collections.Generic; | |
| using AopBlog.Framework; | |
| using DotNetOpenAuth.OAuth.ChannelElements; | |
| using DotNetOpenAuth.OAuth.Messages; | |
| namespace AopBlog.Models.Services | |
| { | |
| public class ConsumerTokenManager : IConsumerTokenManager | |
| { | |
| private readonly Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>(); | |
| public ConsumerTokenManager() | |
| { | |
| ConsumerKey = BlogConfig.TwitterOauthApiKey; | |
| ConsumerSecret = BlogConfig.TwitterOauthSecretKey; | |
| } | |
| #region ITokenManager Members | |
| public string ConsumerKey { get; private set; } | |
| public string ConsumerSecret { get; private set; } | |
| public string GetTokenSecret(string token) | |
| { | |
| return this.tokensAndSecrets[token]; | |
| } | |
| public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response) | |
| { | |
| this.tokensAndSecrets[response.Token] = response.TokenSecret; | |
| } | |
| public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret) | |
| { | |
| this.tokensAndSecrets.Remove(requestToken); | |
| this.tokensAndSecrets[accessToken] = accessTokenSecret; | |
| } | |
| /// <summary> | |
| /// Classifies a token as a request token or an access token. | |
| /// </summary> | |
| /// <param name="token">The token to classify.</param> | |
| /// <returns>Request or Access token, or invalid if the token is not recognized.</returns> | |
| public TokenType GetTokenType(string token) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| #endregion | |
| } | |
| } |
This file contains hidden or 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.Web.Mvc; | |
| namespace AopBlog.Areas.Manage.Controllers | |
| { | |
| public class HomeController : Controller | |
| { | |
| [Authorize] | |
| public ViewResult Index() | |
| { | |
| return View(); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| namespace AopBlog.Models.Services | |
| { | |
| public class OauthAuthenticationToken | |
| { | |
| public bool IsAuthenticated { get; set; } | |
| public string AccessToken { get; set; } | |
| public string ScreenName { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment