private void Initialize() { this.m_login = false; AccountRepository accountRepository = new AccountRepository(); m_accounts = accountRepository.All(); this.m_view = new LoginView(); string account = string.Empty; string password = string.Empty; AppSettings.GetRemember(ref account, ref password); if (m_accounts != null) { try { Account user = m_accounts.First(item => item.Name.Equals(account)); this.m_view.Remember = true; this.m_view.User = account; this.m_view.Password = password; } catch (Exception) { } } this.m_view.OnLogin += view_OnLogin; this.m_view.OnExit += view_OnExit; this.m_view.OnKeyboard += view_OnKeyboard; this.m_view.ViewForm.ShowDialog(); }
public async Task <bool> AddAccount() { try { // http://www.cloudidentity.com/blog/2014/08/26/the-common-endpoint-walks-like-a-tenant-talks-like-a-tenant-but-is-not-a-tenant/ // https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devhowto-multi-tenant-overview // use common auth to allow the user to login to any account. the auth response is valid for the selected account only var userAuth = await AcquireToken("common", PromptBehavior.SelectAccount).ConfigureAwait(false); // the auth points to the accounts home tenant and the access token is valid for that only var accountTenantId = GetTenantId(userAuth.Authority); // check if we know this account already var currAccount = accountRepository.All().SingleOrDefault(x => x.TenantId == accountTenantId && x.Info.UniqueId == userAuth.UserInfo.UniqueId); if (currAccount != null) { return(false); } var userCtx = AddContext(userAuth); // the tenant knowns which other tenants the user is linked to, so query for that var tenantIds = await api.AvailableTenants(userCtx); var tenants = new List <Tenant>(); foreach (var tenantId in tenantIds) { // do separate graph call to obtain the displayname itself. // adal seems confused when using graph api, so here we specify userId, // otherwise when we obtain the token we might obtain it for the wrong account context. var graphAuth = await AcquireToken(tenantId.TenantId, PromptBehavior.Never, userUniqueId : userAuth.UserInfo.UniqueId, useGraphAuth : true) .ConfigureAwait(false); // pass in a one time token, we persist the values returned from the ms graph call try { var tenant = await api.Tenant(new AzureAccessToken { TenantId = graphAuth.TenantId, Value = graphAuth.AccessToken }); var auth = await AcquireToken(tenantId.TenantId, PromptBehavior.Never).ConfigureAwait(false); var subs = await api.Subscriptions(new AzureAccessToken { TenantId = tenantId.TenantId, Value = auth.AccessToken, }); tenant.Subscriptions = subs; tenants.Add(tenant); } catch (UnauthorizedAccessException) { } } var newAccount = new Account { Info = new Core.AzureRM.Models.UserInfo { DisplayableId = userAuth.UserInfo.DisplayableId, UniqueId = userAuth.UserInfo.UniqueId, GivenName = userAuth.UserInfo.GivenName, FamilyName = userAuth.UserInfo.FamilyName }, TenantId = accountTenantId, Tenants = tenants }; var added = accountRepository.Add(newAccount); if (added) { accounts.Add(newAccount); } return(added); } catch (AdalServiceException) // assume user cancelled login { // when (exn.Message.Contains("AADSTS70002") || // Error validating credentials. // exn.Message.Contains("AADSTS50012") || // Invalid client secret is provided. // exn.Message.Contains("User canceled authentication")) // cancelled // { return(false); } }
public ActionResult Index() { var accounts = _accountRepository.All(); return(View(accounts)); }