I am trying to detect a MAC user using c#. I have used the following code but it always says unknown when a mac user navigates to my site. It works great for windows users but not for MAC or anything else. Does anyone have any ideas how to pick up on mac users?
Thanks
HttpBrowserCapabilities moo = HttpContext.Current.Request.Browser;
StringBuilder sb = new StringBuilder();
sb.Append("<p>Browser Capabilities:</p>");
sb.Append("Type = " + moo.Type + "<br>");
sb.Append("Name = " + moo.Browser + "<br>");
sb.Append("Version = " + moo.Version + "<br>");
sb.Append("Major Version = " + moo.MajorVersion + "<br>");
sb.Append("Minor Version = " + moo.MinorVersion + "<br>");
sb.Append("Platform = " + moo.Platform + "<br>");
sb.Append("Is Beta = " + moo.Beta + "<br>");
sb.Append("Is Crawler = " + moo.Crawler + "<br>");
sb.Append("Is AOL = " + moo.AOL + "<br>");
sb.Append("Is Win16 = " + moo.Win16 + "<br>");
sb.Append("Is Win32 = " + moo.Win32 + "<br>");
sb.Append("Supports Frames = " + moo.Frames + "<br>");
sb.Append("Supports Tables = " + moo.Tables + "<br>");
sb.Append("Supports Cookies = " + moo.Cookies + "<br>");
sb.Append("Supports VB Script = " + moo.VBScript + "<br>");
sb.Append("Supports ActiveX Controls = " + moo.ActiveXControls + "<br>");
sb.Append("CDF = " + moo.CDF + "<br>");
You can extract OS information from Request.UserAgent.
Macintosh user agent strings are in this form:
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us)
AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"
"Mozilla/4.0 (compatible; MSIE 5.15; Mac_PowerPC)"
So you could do something like:
public bool IsMacOS(string userAgent)
{
var osInfo = userAgent.Split(new Char[] { '(', ')' })[1];
return osInfo.Contains("Mac_PowerPC") || osInfo.Contains("Macintosh");
}
you can use Request.UserAgent it will return something like this:
"Mozilla/5.0 (Windows; U; Windows NT 5.1; da; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"..
then you will need to extract the OS
This might help you
http://www.javascripter.net/faq/operatin.htm
You should use native ASP.NET browser caps and just extend them.
What you do is just create App_Browsers/BrowserFile.browser file in your ASP.NET application.
And add this to the file:
<browsers>
<gateway id="MacOS" parentID="Safari">
<identification>
<userAgent match="Intel Mac OS X" />
</identification>
<capabilities>
<capability name="platform" value="MacOS" />
</capabilities>
</gateway>
</browsers>
Doing this will be enough for Browser.Platform to return "MacOS"
Related
I'm inputting the following code for detect browser type (IE, FF, Chrome, Edge) used from users, using C# ASPNET.
The detected browser type working but I have verified that when the code is getting from the Microsoft Edge browser the output is
Browser Capabilities
Type = Chrome94
Name = Chrome
Version = 94.0
Major Version = 94
Minor Version = 0
Platform = WinNT
Is Beta = False
Is Crawler = False
Is AOL = False
Is Win16 = False
Is Win32 = True
Supports Frames = True
Supports Tables = True
Supports Cookies = True
Supports VBScript = False
Supports JavaScript = 3.0
Supports Java Applets = True
Supports ActiveX Controls = False
Supports JavaScript Version = 1.7
As if the browser used were Google Chrome.
What could be the problem?
My code below
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
This is what has worked for me in the past:
An accurate browser detection is to use the UserAgent property, as in
HttpContext.Current.Request.Request.UserAgent
If your user agent contains the sub-string "Edg" as in like case:
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52"
Then your browser will be Edge.
If your user agent contains "Chrome" as in this case:
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML,like Gecko) Chrome/93.0.4577.82 Safari/537.36"
Then your browser will be Chrome.
Similarly for Firefox as in this case:
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0"
A property I have tried in the past is this:
HttpContext.Current.Request.Browser.Browser
Which seems like the obvious choice, and will give you the correct browser in many cases, but within an Edge browser, it has given me Chrome. So using the UserAgent string is the best way of obtaining the browser for an ASP.NET HTTP request.
I have network IP address can I know the active user based on the particular IP address
I have under details of the same network
"host"+"-" + new Program().GetHostName(ip[0].ToString()).ToString() + "<br>" +
"KeepAlive" + request.KeepAlive + "<br>" +
"Local end point: {0}" + "-" + request.LocalEndPoint.ToString() + "<br>" +
"Remote end point: {0}" + "-" + "--" + "<br>" +
"Is local? {0}" + "-" + request.IsLocal + "<br>" +
"HTTP method: {0}" + "-" + request.HttpMethod + "<br>" +
"Protocol version: {0}" + "-" + request.ProtocolVersion + "<br>" +
"Is authenticated: {0}" + "-" + request.IsAuthenticated + "<br>" +
"Is secure: {0}" + "<br>" + request.IsSecureConnection + "<br>" +
//"username " + "-"+ username + "<br>"+
I'm assuming you're using an HttpListener/HttpRequestListener here, and if so please see HttpListener: how to get http user and password? - the accepted answer there deals with basic auth but it links to an article that details how to request Windows auth, so the client should quote the logged in windows user name to your app.
Ps: Looks like you're creating a log? That string concat code is a bit of a mess and would benefit from being tidied up, maybe using interpolated strings:
$#"Host: {new Program().GetHostName(ip[0].ToString())}<br/>
KeepAlive: {request.KeepAlive}<br/>
Local end point: {request.LocalEndPoint}<br/>
... and so on ...
Let's say I have A3 and in my code which is executed at Windows Azure I write
int numberOfProcessors = Environment.ProcessorCount;
What will go into numberOfProcessors variable? I assume that numberOfProcessors will be equal to zero because msdn claims that Windows Azure doesn't belong to supported platforms for that function. If I'm correct, then what can I use as replacement Environment.ProcessorCount?
I think that you've read the wrong MSDN :) There is no such information in it about azure. Moreover, there is a lot of articles for ProcessorCountusage in Azure environment:
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
// Show the Environment Information
builder.AppendLine("<h2>Environment Information</h2>");
builder.Append("<b>Machine Name: </b>" + Environment.MachineName + "<br>");
builder.Append("<b>OS Version: </b>" + Environment.OSVersion + "<br>");
builder.Append("<b>Is 64Bit Operating System: </b>" + Environment.Is64BitOperatingSystem + "<br>");
builder.Append("<b>Processor Count: </b>" + Environment.ProcessorCount + "<br>");
builder.Append("<b>User Name: </b>" + Environment.UserName + "<br>");
builder.Append("<b>Is Debugger Attached: </b>" + Debugger.IsAttached + "<br>");
// Show the Process Information
builder.AppendLine("<h2>Processes Information</h2>");
foreach (Process process in Process.GetProcesses())
builder.AppendLine(process.ProcessName + "</br>");
// Show the RoleEnvironment Information
builder.AppendLine("<h2>Role Environment Information</h2>");
builder.Append("<b>Curent Role Instance Name: </b>" + RoleEnvironment.CurrentRoleInstance.Role.Name + "<br>");
builder.Append("<b>Deployment Id: </b>" + RoleEnvironment.DeploymentId + "<br>");
builder.Append("<b>Is Emulated: </b>" + RoleEnvironment.IsEmulated + "<br>");
// Display the Resutls
InfoLabel.Text = builder.ToString();
}
and result for it:
from here. So, it definitely work in Azure WebRole.
But if you are talking about the NumberOfCores, then you have to change it manually - it is administrative function:
I have to do if user's browser compatibility is on then need to show message to user that your browser's compatibility is on.
I have searched this a lot on google but yet not found a proper answer.
I have tried below code but HttpContext.Current.Request.UserAgent always contains MSIE 7.0
string isOn = string.Empty;
if (HttpContext.Current.Request.UserAgent.IndexOf("MSIE 7.0") > -1)
{
isOn = "IE8 Compatibility View";`
}
else
{
isOn = "IE8";
}
}
You may try like this
if (Request.Browser.Type.ToUpper().Contains("IE"))
{
if (Request.Browser.MajorVersion < 7)
{
//Show the message here
}
...
}
else if (Request.Browser.Type.Contains("Firefox"))
{
//code to show message
}
else if (Request.Browser.Type.Contains("Chrome"))
{
//code to show message
}
Also check this MSDN which has its own way of detecting the browser
Query the Browser property, which contains an HttpBrowserCapabilities
object. This object gets information from the browser or client device
during an HTTP request, telling your application the type and level of
support the browser or client device offers. The object in turn
exposes information about browser capabilities using strongly typed
properties and a generic name-value dictionary.
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
TextBox1.Text = s;
}
we have a c# application that reads an email Inbox currently hosted on Exchange 2003 using the http service.
Now the mailbox is to be migrated to an Exchange 2010 server, so we are testing our code to confirm it will still work.
We are getting an error 'Bad request' with the below code (which tries to get all the mail):
public static XmlDocument GetUnreadMailAll()
{
HttpWebRequest loRequest = default(HttpWebRequest);
HttpWebResponse loResponse = default(HttpWebResponse);
string lsRootUri = null;
string lsQuery = null;
byte[] laBytes = null;
Stream loRequestStream = default(Stream);
Stream loResponseStream = default(Stream);
XmlDocument loXmlDoc = default(XmlDocument);
loXmlDoc = new XmlDocument();
try
{
lsRootUri = strServer + "/Exchange/" + strAlias + "/" + strInboxURL;
lsQuery = "<?xml version=\"1.0\"?>"
+ "<D:searchrequest xmlns:D = \"DAV:\" xmlns:m=\"urn:schemas:httpmail:\">"
+ "<D:sql>SELECT "
+ "\"urn:schemas:httpmail:to\", "
+ "\"urn:schemas:httpmail:displayto\", "
+ "\"urn:schemas:httpmail:from\", "
+ "\"urn:schemas:httpmail:fromemail\", "
+ "\"urn:schemas:httpmail:subject\", "
+ "\"urn:schemas:httpmail:textdescription\", "
//+ "\"urn:schemas:httpmail:htmldescription\", "
+ "\"urn:schemas:httpmail:hasattachment\", "
+ "\"urn:schemas:httpmail:attachmentfilename\", "
+ "\"urn:schemas:httpmail:senderemail\", "
+ "\"urn:schemas:httpmail:sendername\", "
+ "\"DAV:displayname\", "
+ "\"urn:schemas:httpmail:datereceived\", "
+ "\"urn:schemas:httpmail:read\", "
+ "\"DAV:id\" "
+ "FROM \"" + lsRootUri
+ "\" WHERE \"DAV:ishidden\" = false "
+ "AND \"DAV:isfolder\" = false "
+ "AND \"urn:schemas:httpmail:read\" = false "
+ "AND \"urn:schemas:httpmail:fromemail\" != 'emailAddy#domainName.co.uk' "
+ "</D:sql></D:searchrequest>";
loRequest = (HttpWebRequest)WebRequest.Create(lsRootUri);
loRequest.Credentials = new NetworkCredential(strUserName, strPassword);
loRequest.Method = "SEARCH";
laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery);
loRequest.ContentLength = laBytes.Length;
loRequestStream = loRequest.GetRequestStream();
loRequestStream.Write(laBytes, 0, laBytes.Length);
loRequestStream.Close();
loRequest.ContentType = "text/xml";
loRequest.Headers.Add("Translate", "F");
loResponse = (HttpWebResponse)loRequest.GetResponse();
loResponseStream = loResponse.GetResponseStream();
loXmlDoc.Load(loResponseStream);
loResponseStream.Close();
}
the exception is thrown on the line loResponseStream = loResponse.GetResponseStream();
here is the xml that we are sending:
<?xml version="1.0" ?>
- <D:searchrequest xmlns:D="DAV:" xmlns:m="urn:schemas:httpmail:">
<D:sql>SELECT "urn:schemas:httpmail:to", "urn:schemas:httpmail:displayto", "urn:schemas:httpmail:from", "urn:schemas:httpmail:fromemail", "urn:schemas:httpmail:subject", "urn:schemas:httpmail:textdescription", "urn:schemas:httpmail:hasattachment", "urn:schemas:httpmail:attachmentfilename", "urn:schemas:httpmail:senderemail", "urn:schemas:httpmail:sendername", "DAV:displayname", "urn:schemas:httpmail:datereceived", "urn:schemas:httpmail:read", "DAV:id" FROM "https://domain/Exchange/bbtest/Inbox" WHERE "DAV:ishidden" = false AND "DAV:isfolder" = false AND "urn:schemas:httpmail:read" = false AND "urn:schemas:httpmail:fromemail" != 'emailAddy#domainName.co.uk'</D:sql>
</D:searchrequest>
and from MSDN the answer is that WebDAV is deprecated after Exchange 2007, and replaced by Exchange Web Services
here are a couple of links:
MSDN Library: Get started with Exchange Web Services
OMEGACODER: Getting all emails from Exchange using Exchange Web Services
MSDN Code Downloads: Exchange - 101 samples