Ejemplo n.º 1
0
        public async Task <ActionResult> Login(string returnUrl)
        {
            string state = Guid.NewGuid().ToString("N");

            _stateCache.Value.Add(new CacheItem(state, returnUrl ?? "/"),
                                  new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10)
            });
            string loginUrl    = Url.Encode(LocalRedirectUrl);
            string linkedInUrl = LinkedInApiService.GetSignInUrl(state, loginUrl);

            return(Redirect(linkedInUrl));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> LoginLinkedIn(string state, string code)
        {
            if (!String.IsNullOrEmpty(Request.QueryString?["error"]))
            {
                string errorMessage = $"{Request.QueryString?["error"]} {Request.QueryString?["error_description"]}";
                Trace.TraceWarning($"Login failed: Invalid login request from LinkedIn sign in. {errorMessage}");
                return(RedirectToAction("ExternalLoginFailure"));
            }

            if (String.IsNullOrEmpty(state) || String.IsNullOrEmpty(code))
            {
                Trace.TraceWarning($"Login failed: Improper state or code provided. state: {state}, code: {code}");
                return(RedirectToAction("ExternalLoginFailure"));
            }
            string returnUrl = _stateCache.Value.Get(state) as string;

            if (returnUrl == null)
            {
                Trace.TraceWarning($"Login failed: Failed to retreive login state from database. state: {state}, code: {code}");
                return(RedirectToAction("ExternalLoginFailure"));
            }

            try
            {
                IncomingApiDataResponse <LinkedInTokenResponse> tokenResponse = await LinkedInApiService.GetToken(LocalRedirectUrl, code).ConfigureAwait(false);

                LinkedInUser user = await _userService.InitializeUserFromLinkedIn(tokenResponse.Data).ConfigureAwait(false);

                List <Claim> claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, $"{user.User.FirstName} {user.User.LastName}"),
                    new Claim(ClaimTypes.Email, user.User.Email),
                    new Claim(ClaimTypes.NameIdentifier, user.LinkedInId),
                    new Claim(AppConstants.CustomClaims.IsdUserId, user.Id.ToString())
                };
                if (!String.IsNullOrEmpty(user.ProfileImageUrl))
                {
                    claims.Add(new Claim(AppConstants.CustomClaims.UserImageUrl, user.ProfileImageUrl));
                }
                ClaimsIdentity identity    = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
                IOwinContext   authContext = Request.GetOwinContext();
                authContext.Authentication.SignIn(new AuthenticationProperties {
                    IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddSeconds(tokenResponse.Data.expires_in)
                }, identity);
                return(RedirectToLocal(returnUrl));
            }
            catch (LinkedInApiResponseException ex)
            {
                Trace.TraceError(ex.ToString(), ex);
                return(RedirectToAction("ExternalLoginFailure"));
            }
        }