58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
|
|
namespace Taloyhtio.GeneralSSO.Server.CodeFiles.Common
|
|
{
|
|
public static class Utils
|
|
{
|
|
// See example from Pro ASP.Net Web API Security, Chapter 6
|
|
public static X509Certificate2 GetCert(string subjectName, StoreName storeName, StoreLocation storeLocation)
|
|
{
|
|
X509Store store = null;
|
|
try
|
|
{
|
|
store = new X509Store(storeName, storeLocation);
|
|
store.Open(OpenFlags.ReadOnly);
|
|
var cert = store.Certificates.OfType<X509Certificate2>().FirstOrDefault(
|
|
c =>
|
|
{
|
|
string name = c.SubjectName.Name;
|
|
if (name.StartsWith("CN="))
|
|
{
|
|
name = name.Substring(3);
|
|
}
|
|
return string.Compare(name, subjectName, true) == 0;
|
|
});
|
|
return (cert != null ? new X509Certificate2(cert) : null);
|
|
}
|
|
finally
|
|
{
|
|
if (store != null)
|
|
{
|
|
store.Certificates.OfType<X509Certificate2>().ToList().ForEach(c => c.Reset());
|
|
store.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get user name without membership provider prefix
|
|
public static string GetUserName(string loginName)
|
|
{
|
|
if (string.IsNullOrEmpty(loginName))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
int idx = loginName.IndexOf(":");
|
|
if (idx > 0)
|
|
{
|
|
loginName = loginName.Substring(idx + 1);
|
|
}
|
|
return loginName;
|
|
}
|
|
}
|
|
}
|