Sharing key chain variable in Xamarin - c#

We have an iOS app that is built in different platform(swift,xamarin). We have a requirements that required those app to share variables using keychain. The keychain sharing is working for swift to swift App but for swift to Xamarin App it is not working. Possible something wrong with my code. I don't see much documentation in Xamarin on how to access keychain.
SecStatusCode res;
var rec = new SecRecord(SecKind.GenericPassword)
{
Generic = NSData.FromString("sharedKey"),
AccessGroup = ""
};
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (match != null)
{
var dt = match.ValueData.ToString();
return dt;
}

This might be caused by your Xamarin app not being access the keychain.
I don't know if you have this implemented, but if this is missing, try to Enable Keychain Access in your Xamarion iOS application.

I figure out my issue.
SecStatusCode res;
var rec = new SecRecord(SecKind.GenericPassword)
{
AccessGroup = "Shared access group example MC123.com.test.shared",
Account = "SharedKey that you are looking for"
};
var match = SecKeyChain.QueryAsRecord(rec, out res);
if (match != null)
{
var dt = match.ValueData.ToString();
return dt;
}

Related

BLE host with Xamarin

I need an application which is use for advertise some services.
I installed some BLE plugin from NUGet and ı readed their documentation on github. Generally they explain scanner mode, except this plugin of BluetoothLE ( https://github.com/aritchie/bluetoothle) .
I tried that advertiser code :
protected override void OnStart()
{
var server = CrossBleAdapter.Current.CreateGattServer();
var service = server.AddService(Guid.NewGuid(), true); //ı got error on this line
var characteristic = service.AddCharacteristic(
Guid.NewGuid(),
CharacteristicProperties.Read | CharacteristicProperties.Write ,
GattPermissions.Read | GattPermissions.Write
);
var notifyCharacteristic = service.AddCharacteristic
(
Guid.NewGuid(),
CharacteristicProperties.Indicate | CharacteristicProperties.Notify,
GattPermissions.Read | GattPermissions.Write
);
IDisposable notifyBroadcast = null;
notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e =>
{
var #event = e.IsSubscribed ? "Subscribed" : "Unsubcribed";
if (notifyBroadcast == null)
{
this.notifyBroadcast = Observable
.Interval(TimeSpan.FromSeconds(1))
.Where(x => notifyCharacteristic.SubscribedDevices.Count > 0)
.Subscribe(_ =>
{
Debug.WriteLine("Sending Broadcast");
var dt = DateTime.Now.ToString("g");
var bytes = Encoding.UTF8.GetBytes(dt);
notifyCharacteristic.Broadcast(bytes);
});
}
});
characteristic.WhenReadReceived().Subscribe(x =>
{
var write = "HELLO";
// you must set a reply value
x.Value = Encoding.UTF8.GetBytes(write);
x.Status = GattStatus.Success; // you can optionally set a status, but it defaults to Success
});
characteristic.WhenWriteReceived().Subscribe(x =>
{
var write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
// do something value
});
}
Error is that;
CS1061 'IObservable' does not contain a definition of 'AddService' and no accessible extension methods 'AddService' were found that accept a first argument of type 'IObservable' (could you be missing a using directive or assembly reference?)
What can ı do about that? Do you know another plugin can do advertiser ?
That plugin you are using is now out of service from reading the notice on the link you included. They have now shifted the functionality over to Shiny
I would strongly recommend looking at Shiny as it has some nice documentation on how to get setup with BLE Hosting here:
https://shinylib.net/blehosting/
Given that Shiny is written by the same person that wrote the plugin you were initially trying and looking over the documentation it shouldn't be too difficult to shift to this approach as large amounts of the code looks similar if not the same.

C# code works from Console function but does not work in SQL CLR Stored Procedure

PLEASE HELP!!! I have a code to get data from AD. This used to work in SQL2014/Visual Studio2013. Now, we are migrating to SQL2016. I tested the code in a Console App and it worked just fine. It just does not work when I create the same code into a SQL CLR Stored Proc using Visual Studio 2017.
This is the code in the Console App:
static void Main(string[] args)
{
DirectoryEntry ldapConnection;
DirectorySearcher search;
SearchResult result;
DirectoryEntry ldapconnection = null;
string szJDEID = "";
string szErrorMessage = "";
string szNetworkID = "xyz";
//--- Create and return new LDAP connection with desired settings
ldapConnection = new DirectoryEntry("LDAP://DC.company.com:389", "userid", "password");
ldapConnection.Path = "LDAP://DC=company,DC=com";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
//--- create search object which operates on ldap connection object and set search object to only find the user specified
search = new DirectorySearcher(ldapconnection);
search.Filter = "(&(samaccountname=" + szNetworkID.Trim() + ")(memberOf=CN=JDEdwards Users,OU=Mail Enabled Manual,OU=Groups,OU=Global,DC=company,DC=com))";
result = search.FindOne();
if (result != null)
{
//--- create an array of properties that we would like and add them to the search object
string[] requiredproperties = new string[] { "extensionattribute13" };
foreach (string property in requiredproperties)
search.PropertiesToLoad.Add(property);
result = search.FindOne();
if (result != null)
{
foreach (string property in requiredproperties)
foreach (object mycollection in result.Properties[property])
szJDEID = mycollection.ToString();
}
}
else
{
szErrorMessage = "ERROR: This user does not belong to the JDEdwards Users AD Group. Please check with the IT Helpdesk team.";
}
}
I get the value of szJDEID as stored in Extension Attribute13. When I put the same code in a SQL CLR Stored Proc, the Logic always returns the szErrorMessage value.
What am I missing?
Thanks in advance.
Finally. As you had rightly pointed earlier, the bindDn was incorrect. The issue is we moved from one DC to another. I used lip.exe to find out the principal - userid#domain.com. Also, the ldapConnection.Path was not needed anymore as it was incorrect with the new DC. So, finally, it is working. Thanks Clint.

Can't retrieve application rules with HNetCfg.FwMgr + AuthorizedApplications

I'm trying to retrieve all authorized applications with C#:
ArrayList result = new ArrayList();
INetFwMgr firewallManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
foreach (INetFwAuthorizedApplication app in firewallManager.LocalPolicy.CurrentProfile.AuthorizedApplications)
{
Console.WriteLine(app.Name);
}
The AuthorizedApplications is empty, but in Control Panel I can see many rules, enabled or not:
What was wrong? I tried other profiles, e.g DOMAIN/STANDARD, same result.
console.write(result); check if forEach method brings you all aplications authorized with C# should be a loop instead
foreach (INetFwAuthorizedApplication app in
firewallManager.LocalPolicy.CurrentProfile.AuthorizedApplications)
{
Console.WriteLine(app.Name);
}e.g for(INetFwAuthorizedApplication app in
firewallManager.LocalPolicy.CurrentProfile.AuthorizedApplications){console.log(app.Name);} Are you sure Name is the correct value?, shouldn't be app.name?
It seems you can iterate the app list, but you are not finding the correct name for app. Use name instead Name. Try console.WriteLine(app.name) or console.WriteLine(app.GetName)
Maybe try this:
public List<Object>(String name){
ArrayList result = new ArrayList();
INetFwMgr firewallManager =
(INetFwMgr)Activator.CreateInstance
(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
foreach (INetFwAuthorizedApplication app in
firewallManager.LocalPolicy
.CurrentProfile.AuthorizedApplications)
{
if(app.Name == _name){
app.Name = name;
Console.WriteLine(app.Name);
}
}
return result;
}

Tweetsharp - Oauthaccestoken overflowexception

I've been stuck for a while on the following problem when I debug the following code:
TwitterService service = new TwitterService("_consumerkey", "_consumersecret");
OAuthRequestToken requestToken = service.GetRequestToken();
Uri uri = service.GetAuthorizationUri(requestToken);
Process.Start(uri.ToString());
Console.Write("Verificatiecode? ");
string verifier = Console.ReadLine();
OAuthAccessToken access = service.GetAccessToken(requestToken, verifier);
service.AuthenticateWith(access.Token, access.TokenSecret);
TwitterUser twitterUser = service.GetUserProfile(new GetUserProfileOptions());
ListFriendsOptions friends_options = new ListFriendsOptions();
friends_options.UserId = twitterUser.Id;
friends_options.Cursor = -1;
var friends = service.ListFriends(friends_options);
do
{
if (friends_options.Cursor != null)
{
foreach (var friend in friends) {Console.WriteLine(friend.ScreenName);}
friends_options.Cursor = friends.NextCursor;
}
} while (friends_options.Cursor != null);
Console.ReadKey(true);
I always get an overflow exception after filling in the verification code here:
OAuthAccessToken access = service.GetAccessToken(requestToken, verifier);
Anyone who can help me?
Thanks in advance
Looking at the source, it seems like the problem is when it tries to return the results inside GetAccessToken:
return new OAuthAccessToken()
{
Token = nameValueCollection["oauth_token"] ?? "?",
TokenSecret = nameValueCollection["oauth_token_secret"] ?? "?",
//this is the only place a conversion to int is occurring that I've found
UserId = Convert.ToInt32(nameValueCollection["user_id"] ?? "0"),
ScreenName = nameValueCollection["screen_name"] ?? "?"
};
Looking on Github, it seems this update might solve the problem.
Download the last version of TweetSharp, old version has user_id as Int32, but new version as Int64 https://github.com/danielcrenna/tweetsharp
This happens because Twitter introduced 64bit user ids a while back.
Older Twitter accounts still have the 32-bit Ids and TweetSharp works just fine with them. But if you opened an account recently you already might have a 64 bit ID and Tweet Sharp fails.
I fixed the problem by getting the tweetsharp-unofficial package from NuGet.

CanvasAuthorize C# SDK Infinite Loop

I'm looking to try and get an MVC3 Canvas app working with the Facebook C# SDK, but am struggling to allow permissions - Below is my code, and when I open the app I get the 'Allow / Deny' dialog but when I click allow I get redirected to my app and the same dialog appears again (And again and so on no matter how many times I click allow)?
I guess I am missing something obvious... If I take the user_groups permission out it works fine, I just can't access the persons groups.
[CanvasAuthorize(Permissions = "user_groups")]
public class HomeController : Controller
{
public ActionResult Index()
{
IFacebookApplication settings = FacebookApplication.Current;
if (settings != null)
{
//CanvasPage = settings.CanvasPage;
//AppId = settings.AppId;
}
FacebookWebContext facebookContext = FacebookWebContext.Current;
FacebookSignedRequest signedRequest = facebookContext.SignedRequest;
var client = new FacebookWebClient(facebookContext.AccessToken);
dynamic me = client.Get("me");
var friends = client.Get("me/friends");
var groups = client.Get("me/groups");
ViewBag.Name = me.name;
ViewBag.Id = me.id;
JavaScriptSerializer sr = new JavaScriptSerializer();
var fbFriends = sr.Deserialize<FBFriends>(friends.ToString());
ViewData["friends"] = fbFriends.data;
return View("Friends");
}
}
Any help, tips or code samples greatly appreciated.
make sure u have set the appid and appsecret correctly.
download the source code and checkout the "samples" folder, there are a bunch of asp.net mvc samples.

Categories