Exemplo n.º 1
0
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
            {
                ClientId              = clientId,
                Authority             = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                    AuthorizationCodeReceived = (context) =>
                    {
                        var code = context.Code;
                        ClientCredential credential       = new ClientCredential(clientId, clientSecret);
                        string signedInUserID             = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                        AuthenticationContext authContext = new AuthenticationContext(authority);
                        AuthenticationResult result       = authContext.AcquireTokenByAuthorizationCodeAsync(
                            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, resource).Result;

                        // Store the access token for the user in the cache, because at this point we do not have a session cache.
                        AccessTokenCache.SetItem(signedInUserID, result.AccessToken);

                        return(Task.FromResult(0));
                    }
                }
            });
        }
Exemplo n.º 2
0
        internal BearerTokenAuthenticationPolicy(TokenCredential credential, IEnumerable <string> scopes, TimeSpan tokenRefreshOffset, TimeSpan tokenRefreshRetryDelay)
        {
            Argument.AssertNotNull(credential, nameof(credential));
            Argument.AssertNotNull(scopes, nameof(scopes));

            _accessTokenCache = new AccessTokenCache(credential, scopes.ToArray(), tokenRefreshOffset, tokenRefreshRetryDelay);
        }
Exemplo n.º 3
0
        public HttpResponseMessage GetLogin(string Email, string Password, Guid AppKey)
        {
            string strJson  = string.Empty;
            var    response = this.Request.CreateResponse(HttpStatusCode.OK);

            try
            {
                using (TTPAPIDataContext DB = new TTPAPIDataContext())
                {
                    UserManagement objuser     = new UserManagement();
                    var            appKeycheck = DB.AccountManagemets.Where(x => x.AppKey == AppKey).FirstOrDefault();
                    if (appKeycheck != null)
                    {
                        var logininformation = (from objUserContactDets in DB.UserContactDets
                                                join objUserLoginDet in DB.UserLoginDets on objUserContactDets.UserId equals objUserLoginDet.UserId
                                                join objUserManagement in DB.UserManagements on objUserContactDets.UserId equals objUserManagement.UserId
                                                join objaccount in DB.AccountManagemets on objUserManagement.AccountID equals objaccount.AccountID
                                                where objUserContactDets.EmailAddress == Email && objUserLoginDet.Password == Password && objaccount.AppKey == AppKey
                                                select new
                        {
                            UserId = objUserManagement.UserId,
                            Token = String.Format("{0}{1}{2}", objUserManagement.UserId.ToString(), Convert.ToString(objUserManagement.AccountID), Convert.ToString(objUserManagement.RoleId)),
                        }).FirstOrDefault();
                        if (logininformation != null)
                        {
                            AccessTokenCache objAccessTokenCache = new AccessTokenCache();
                            var checkappkey = DB.AccessTokenCaches.Where(x => x.Token == logininformation.Token).FirstOrDefault();
                            if (checkappkey == null)
                            {
                                objAccessTokenCache.UserId             = logininformation.UserId;
                                objAccessTokenCache.Token              = logininformation.Token;
                                objAccessTokenCache.LastAccessDateTime = DateTime.Now;
                                DB.AccessTokenCaches.InsertOnSubmit(objAccessTokenCache);
                                DB.SubmitChanges();
                            }
                            strJson          = "{\"Token\":\"" + logininformation.Token + "\"}";
                            response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                            return(response);
                        }
                        else
                        {
                            strJson          = "{\"Result\":\"100\"}";
                            response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                            return(response);
                        }
                    }
                    else
                    {
                        strJson          = "{\"Result\":\"Invalide AppKey\"}";
                        response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                strJson          = "{\"Result\":\"" + ex.Message + "\"}";
                response.Content = new StringContent(strJson, Encoding.UTF8, "application/json");
                return(response);
            }
        }