Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all 8156 articles
Browse latest View live

Getting User based bearer token

$
0
0

How can i create a user based bearer token using username of the user.

I created it following way. But for that it needs user password. i need to do it using only user name.

           

private string GetUserAccessToken()
        {
            string clientId = "XXXX";
            string appKey = "XXXX";
            string tenantId = "XXXX";

            string accessToken = string.Empty;
            string apiEndpoint = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
            WebRequest accessTokenRequest = WebRequest.Create(apiEndpoint);
            accessTokenRequest.Method = "POST";
            accessTokenRequest.ContentType = "application/x-www-form-urlencoded";

            string requestParams = "grant_type=password&client_id=" + clientId + "&client_secret=" + appKey + "&scope=https://graph.microsoft.com/.default" + "&userName=XXXX&password=XXXX";
            byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);
            accessTokenRequest.ContentLength = byteArray.Length;
            Stream dataStream = accessTokenRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            using (WebResponse response = accessTokenRequest.GetResponse())
            {
                string json = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    json = reader.ReadToEnd();
                }
                O365AccessTokenModel accessTokenModel = JsonConvert.DeserializeObject<O365AccessTokenModel>(json);
                accessToken = accessTokenModel.access_token;
            }
            return accessToken;
        }

But i need to do it without password. is there ant possible way to do it.


Visual studio 2019

$
0
0

Hi good evening 

Actually I'm a beginner  want to know about Coded UI, In which feature coded UI is available in Visual studio 2019 .

SaveFileDialog does not handle longpath format

$
0
0

Targeting core 3.1 on windows 10

Setting a Microsoft.Win32.SaveFileDialog.InitialDirectory = "\\?\C:\" results in the error:

System.ArgumentException: Value does not fall within the expected range.
  at at MS.Internal.Interop.HRESULT.ThrowIfFailed()
  at at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
  at at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
  at at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
  at at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
  at at Microsoft.Win32.CommonDialog.ShowDialog()

This is a legitimate path according to the documentation https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names

Pasting it in explorer works, Directory.Exists("\\?\C:\") returns true.

saveFileDialog.CheckPathExists returns false after this is set.

I understand if this is a compatibility issue and it cant work because this is a win32 library, but as far as I understand there is no new version of SaveFileDialog that can be substituted to work around this in WPF.

Is there some trick I can use to deal with this? I could manually parse the path and remove the \\?\ prefix but then if the path really is long it would (probably?) still crash.


The type or namespace name 'MultilineStringEditor' could not be found ...

$
0
0

I have no trouble using MultilineStringEditor in my windows forms app. When I tried to create a custom text box class with that  in a library dll with same reference added, I get

Error	CS0246	The type or namespace name 'MultilineStringEditor' could not be found (are you missing a using directive or an assembly reference?)	mywindowsCtlLib	...
what do I need to do?

just a test

OUTLOOK.EXE Getting crashed Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll

$
0
0

While Launching outlook its getting crashed .

Event viewer logs:

Faulting application name: OUTLOOK.EXE, version: 15.0.5075.1001, time stamp: 0x5ba35dd6
Faulting module name: clr.dll, version: 4.8.3928.0, time stamp: 0x5d3900c0
Exception code: 0xc0000005
Fault offset: 0x004d4531
Faulting process id: 0x1c60
Faulting application start time: 0x01d66c15f5f6b76d

Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Report Id: 4651b6ff-d809-11ea-82fa-000c29847be9
Faulting package full name:
Faulting package-relative application ID:

Is there documentation describing Programming with Components for newest version of VS

$
0
0

I have found a document in ms doc site, but it is marked: no longer updating. I understand that this page is ready to be deprecated in the future.

Thanks

just a test


ParallelFor bug

$
0
0

Prepare a static class, with a static property, static method, and a static constructor, then the bug comes:

public class ParallelForBug
{
    public static string StaticProperty = "StaticProperty";

    static void method(int i)
    {
        Console.WriteLine("null method: " + i);
    }

    static ParallelForBug()
    {
        Parallel.For(0, 10, i => {
            Console.WriteLine(i);
            method(i);
        });

    }
}

call the property.

    static void Main(string[] args)
    {
        string str = ParallelForBug.StaticProperty;
        Console.WriteLine(str);            
    }

You will never saw what the str is. Is this a bug, or just I miss anything.


Mike Feng
Help me, help you

What registry keys are responsible for file extension association?

$
0
0
I'm trying to associate file extension to start some type of files with my application. Which registry keys I have to modify to be sure, that when user double click at file it opens with my application.
And how get delete/modify permisions to key "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<extension>\UserChoice"?
This key created after setting default program by "Open with" dialog.

I've tried:
string ext=".eee";

RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\"+ext);

RegistryPermission f = new
RegistryPermission(RegistryPermissionAccess.Write, r.Name);
f.Assert();
r.OpenSubKey("UserChoice", true); //at this step "Requested registry access is not allowed." exception occured

Cannot connect to Redis hosted on local docker from a .Net Core MVC Application on my local machine

$
0
0

I have Redis hosted on my local docker (Port: 6379). I have created a .Net Core app on my local visual studio (note: the mvc app is not in docker) and trying to connect to the redis instance hosted on docker but getting the below exception:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (It was not possible to connect to the redis server(s). UnableToConnect on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 1s ago, last-write: 1s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 8s ago, v: 2.1.58.34321)
  Source=System.Private.CoreLib

Tried using both distributed cache using Microsoft.Extensions.Caching.StackExchangeRedis as well as StackExchange.Redis. Both failing to connect from my app.

But if I login to docker CLI I can connect to Redis and add a key and retrieve value from cache.

Any help is much appreciated..


ParallelFor bug

$
0
0

Prepare a static class, with a static property, static method, and a static constructor, then the bug comes:

public class ParallelForBug
{
    public static string StaticProperty = "StaticProperty";

    static void method(int i)
    {
        Console.WriteLine("null method: " + i);
    }

    static ParallelForBug()
    {
        Parallel.For(0, 10, i => {
            Console.WriteLine(i);
            method(i);
        });

    }
}

call the property.

    static void Main(string[] args)
    {
        string str = ParallelForBug.StaticProperty;
        Console.WriteLine(str);            
    }

You will never saw what the str is. Is this a bug, or just I miss anything.


Mike Feng
Help me, help you

Related to salt encryption(Salt rotation)

$
0
0

I am encrypting a string based on the salt key.

iam thinking to rotate the salt key,if i rotate the salt key then the strings which are encrypted using old salt key should be decrypted after salt key rotation.

Want to understand is that possible, if not what are the other options i have?

ParallelFor bug

$
0
0

Prepare a static class, with a static property, static method, and a static constructor, then the bug comes:

public class ParallelForBug
{
    public static string StaticProperty = "StaticProperty";

    static void method(int i)
    {
        Console.WriteLine("null method: " + i);
    }

    static ParallelForBug()
    {
        Parallel.For(0, 10, i => {
            Console.WriteLine(i);
            method(i);
        });

    }
}

call the property.

    static void Main(string[] args)
    {
        string str = ParallelForBug.StaticProperty;
        Console.WriteLine(str);            
    }

You will never saw what the str is. Is this a bug, or just I miss anything.


Mike Feng
Help me, help you

Connection between two server failing And Windows scheduler not working properly

$
0
0

I have this scenario

1. Server 1 is in the "EX1" domain using "EX1/User.test" to login

2. Server 2 is in the "RT1" domain using "RT1/RT_User.test" to login

Both server does not trust each others, for security concerns, that's the way we are.

We created a network path on Server 1 to be accessed by "EX1/User.test" from Server 2, and it works, when I save the credentials on Windows Credentials Manager.

Then, we create a script which will be pushing files from Server 2 to Server 1, when I run the script manually it works good, but once we add this script to windows scheduler it fails

1. Seems like windows scheduler is using the local user instead of the one stored on windows credentials, and did not find a way to change due to the other domain is not accessible/visible

2. I proceed to create a c# app, using the local user, I read the file and convert to a stream reader, and making Impersonation, login to server 2 using the credentials for it, and still failing, the error is "Bad user or wrong password", seems like it's not accessing the other domain some way like windows does.

Any suggestion ?



How do I get CPU usage from WMI?

$
0
0

Hi people,

I started using WMI to get Drive, Memory, and CPU information. But I am not sure or don't know how to get CPU usage. This is part of a system monitoring functionality I am trying to implement. I need CPU's usage like the same as you get from Task Manager(in percentage). I don't need CPU's temperature.

Also, I need total memory(as in RAM). This is the same RAM from My Computer >> Properties. Example: 2.99GB of RAM. I got FreePhysicalMemeory and FreeVirtualMemeory from Win32_OperatingSystem but I still need the total RAM. I am thinking FreeVirtualMemory is from HardDrive and FreePhysicalMemory is from the RAM that is not being used, and thus free for use.

BlueMarker

How to wait for a Serial Port to respond

$
0
0

Hi

Is it to possible to wait for a serialPort component to complete it's response? 

I have a device connected to the serial port of my PC. I use the serialPort component in order to communicate with the device. I have to send 2 different strings of data to the serial port but the trick is that after sending the first string of data I have to wait for the serial port to respond. Then I have to check the response and decide whether I must send the second string or not. Also after sending the second string, I have to check also the response of the serial port.

I tried to use an arbitrary amount of wait time between the sending of the two strings. Something like

SerialPort.Write(...) ; Thread.Sleep(2000); SerialPort.Write(...);

But the software failed when I tried it in a slower PC. So is there a way to replace the Thread.Sleep(2000) with a command that will wait until the serial port complete its response?

Although, the response of the serial port is different between the two sending strings, I do know that the first response is only one byte and I know the ending byte of the second response. Also, the second response is no more than 30 bytes in length.

Regards

Dimitris

FileSystemSecurity.GetAccessRules does not return all rules

$
0
0

Hi,

I'm trying to find files and directories with an ACE that points to an already deleted user. In Windows Explorer, in the file properties windows, on the security tab, that user's SID is shown because the name cannot be resolved anymore. Though it's correctly listed.

There is this piece of code:

sec = file.GetAccessControl()                                    ''File being a System.IO.FileSystemInfo object
rules = sec.GetAccessRules(True, True, _TypeSecurityIdentifier)  ''_TypeSecurityIdentifier = GetType(System.Security.Principal.SecurityIdentifier)

At runtime, 'file' points to one of the concerned files. In Windows Explorer, four access rules are listed.

My problem is: The 'rules' collection above returns only three of them. The one referring to the deleted user is not included. Why? What can I do to make GetAccessRules returnall existing rules? Intentionally specifying the SecurityIdentifier type as the 3rd argument instead of the NTAccount type obviously doesn't help.

Thanks


Armin

NETframework 3.5

$
0
0

Should Net framework 3..5 be checked in windows 10 pro 2004  by default.

I don't know if I clicked it or not trying to activate sandbox.?

It is currently unchecked and running OK.

Question about Numeric Formatting not seen in documentation

$
0
0

Visual Studio 2019 and .NET allow numbers to be declared with underline spacing for readability.  For example,

   double d = 1234.567_891_23

If output using:

   Console.WriteLine ($"d = {d:N10}");

It displays as: 1,234.5678912300

I've looked through the Standard Numeric Format Strings documentation, but I can't find a format that would display the output from WriteLine as: 1,234.567_891_230_0

Is there a way to do that?

Viewing all 8156 articles
Browse latest View live