本篇文章给大家谈谈获取Windows凭据用作CRM的ClientCredentials,以及System.ServiceModel.ClientCredentials的知识点,同时本文还将给你拓展10
本篇文章给大家谈谈获取Windows凭据用作CRM的ClientCredentials,以及System.ServiceModel.ClientCredentials的知识点,同时本文还将给你拓展10.第三方ClientCredential模式调用、asp.net core IdentityServer4 实现 Client credentials(客户端凭证)、asp.net-mvc – 与SmtpClient.UseDefaultCredentials属性混淆、c# – 任务/线程中的CredentialsCache.DefaultCredentials等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 获取Windows凭据用作CRM的ClientCredentials(System.ServiceModel.ClientCredentials)(获取windows凭证管理器明文密码)
- 10.第三方ClientCredential模式调用
- asp.net core IdentityServer4 实现 Client credentials(客户端凭证)
- asp.net-mvc – 与SmtpClient.UseDefaultCredentials属性混淆
- c# – 任务/线程中的CredentialsCache.DefaultCredentials
获取Windows凭据用作CRM的ClientCredentials(System.ServiceModel.ClientCredentials)(获取windows凭证管理器明文密码)
我正在制作一个简化一些CRM帐户数据操作的Web表单。 我希望我的用户能够使用他们的Windowslogin连接到CRM,而不必键入它。
有没有办法获得用户的Windows凭据与.NET Web应用程序用于CRM身份validation?
现在我正在使用以下内容:
ClientCredentials Credentials = new ClientCredentials(); Credentials.UserName.UserName = "hardcoded username"; Credentials.UserName.Password = "hardcoded password";
但我想自动化这一点。 谢谢!
等待networkingconfiguration在Windows服务启动完成
高精度的睡眠/等待.net
WIX相对path过长
如何在一个应用程序中透明地将多个.Net版本定位到用户?
如何在通知区域创build丰富的工具提示和丰富的气球
.net安装项目强制重新安装新的文件replace
SetSystemTime kernel32中的奇怪行为
无法连接到ABB Freelance OPC服务器
SSL证书validation – 是否涉及任何caching?
了解.NET应用程序内存大小
尝试这个:
service.Credentials = System.Net.CredentialsCache.DefaultNetworkCredentials;
您的Web应用程序必须配置为Windows身份验证。
10.第三方ClientCredential模式调用
10.第三方ClientCredential模式调用
IdentityModel的官方文档:
https://identitymodel.readthedocs.io/en/latest/index.html
ThirdPartyDemo
创建第三方的应用程序,相当于它来请求我们的API
创建控制台的应用程序
dotnet new console --name ThirdPartyDemo:常见控制台程序ThirdPartyDemo
添加nuget包:IdentityModel
首先我们需要访问以下这个IdentityServer,是否可以来实现
运行测试
运行:IdentityServerSample
D:\MyDemos\jesse\IdentityServerSample\IdentityServerCenter
在运行:ClientCredentialApi
D:\MyDemos\jesse\ClientCredentialApi
在运行我们的控制台应用程序
修正代码
继续代码,运行结果
这样access_token就返回了。还有我们的api/Values里面输出的值
D:\MyDemos\jesse\ThirdPartyDemo>dotnet run
Program.cs(11,24): warning CS0618: ‘“discoveryClient”已过时:“This type will be deprecated or changed in a future version. It is recommended that you switch to the new extension methods for HttpClient. They give you much more control over the HttpClient lifetime and configuration. See the docs here: https://identitymodel.readthedocs.io” [D:\MyDemos\jesse\ThirdPartyDemo\ThirdPartyDemo.csproj]
Program.cs(16,35): warning CS0618: ‘“TokenClient”已过时:“This type will be deprecated or changed in a future version. It is recommended that you switch to the new extension methods for HttpClient. They give you much more control over the HttpClient lifetime and configuration. See the docs here: https://identitymodel.readthedocs.io” [D:\MyDemos\jesse\ThirdPartyDemo\ThirdPartyDemo.csproj]
{
"access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImE3MGZkOGQyYjVjMmVlNDUzMWU1ZGUyNWJmYTViNmE4IiwidhlwIjoiSldUIn0.eyJuYmYiOjE1NTIyNzk3MzAsImV4cCI6MTU1MjI4MzMzMCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkiXSwiY2xpZW50X2lkIjoiY2xpZW50Iiwic2NvcGUiOlsiYXBpIl19.hPUdubWxUd2R3xHb4rfGgLFx4Y5KtfK3NFLf3pICzyYnpI8gcfvyzwzWFkY1ZNCwDwq8KVGZsPe_Yu6hRYvlVk8-vsJTXC4W0UJnfEmlFABDfXkao_LlyVZ7ULksg8gZbPje0AVqLtiyKWl66E4iYvsBRfuTBsTVYzStFO4-g2GNsfsyK6rc0iugQo9Gw9hQG3wpumvnq7LJI2SG42GzoGqhWbHvAj5JLvmOY5Mh0ccNR971Z4Q97pp_DXoFSqaLIPfuLN3gD16iQVMzSGMv6tawtpoGZu3XygpOR6T70rXBNTw0StWSdXGyrvI5j3ROKgWy8m9QflzaWr3ElN9Qzw",
"expires_in": 3600,
"token_type": "Bearer"
}
["value1","value2"]
流程
1.先检测地址是否正常:
2.然后通过tokenClient去拿到我们的token
3.拿到token后用httpClient访问我们的API
这就是通过第三方应用程序代码的形式,实现了API的token的获取和api的请求
using System; using IdentityModel.Client; using System.Net.Http; namespace ThirdPartyDemo { class Program { static void Main(string[] args) { var diso = discoveryClient.GetAsync("http://localhost:5000").Result; if (diso.IsError) { Console.WriteLine(diso.Error); } var tokenClient = new TokenClient(diso.TokenEndpoint,"client","secret"); var tokenResponse = tokenClient.RequestClientCredentialsAsync("api").Result; if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); } else { Console.WriteLine(tokenResponse.Json);//输出返回的json } //获取到token 相当于从认证中心拿到了许可 再去访问 api的程序 var httpClient = new HttpClient(); httpClient.SetBearerToken(tokenResponse.Accesstoken); var respone = httpClient.GetAsync("http://localhost:5001/api/values").Result; if (respone.IsSuccessstatusCode) { Console.WriteLine(respone.Content.ReadAsstringAsync().Result); } Console.WriteLine(); } } }
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
asp.net core IdentityServer4 实现 Client credentials(客户端凭证)
前言
OAuth 2.0默认四种授权模式(GrantType)
- 授权码模式(authorization_code)
- 简化模式(implicit)
- 密码模式(resource owner password credentials)
- 客户端模式(client_credentials)
本章主要介绍客户端模式(client credentials) ,他主要是由两部分构成客户端和认证服务器. 认证服务器在确定客户端信息无误后向客户端返回token,客户端请求资源时带着该token进行访问.(在这种模式中用户可直接向客户端注册,客户端再以自己的名义请求认证服务器)
搭建认证服务器
创建一个Api项目工程,端口设置为5000
Package
PM> Install-package IdentityServer4 -version 2.5.3
创建一个类Config(配置要保护的资源和可以访问该API的客户端服务器)
/// <summary>
/// Identity配置
/// </summary>
public class Config
{
/// <summary>
/// 定义要保护的资源
/// </summary>
/// <returns></returns>
public static IEnumerable<ApiResource> GetApiResources() {
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
}
/// <summary>
/// 定义授权客户端
/// </summary>
/// <returns></returns>
public static IEnumerable<Client> GetClients() {
return new List<Client>
{
new Client()
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials, //设置模式,客户端模式
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}
}
配置Startup
在ConfigureServices方法中注入IdentityServer4服务
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddIdentityServer()//IdentityServer4服务
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources()) //配置资源
.AddInMemoryClients(Config.GetClients());//把配置文件的Client配置资源放到内存
}
在Configure方法中添加IdentityServer4服务中间件
app.UseIdentityServer();
搭建 Api Resource
创建一个客户端工程项目,端口设置为5001
Package
PM> Install-package IdentityServer4.AccessTokenValidation -version 2.7.0
配置Startup
在ConfigureServices添加认证服务器地址
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";//授权服务器地址
options.RequireHttpsMetadata = false;//不需要https
options.ApiName = "api1";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
在Configure方法中添加IdentityServer4服务中间件
app.UseIdentityServer();
测试
在客户端values控制器上面增加[Authorize]
直接访问资源服务器http://localhost:5001/api/values
访问受限了 code 401
启动授权服务器
http://localhost:5000/.well-known/openid-configuration
发现端点可通过/.well-known/openid-configuration
获取token
启动后我们通过token_endpoint获取token
client_id为我们在授权服务器配置的clientid, client_secret为配置中的secret, grant_type为授权模式此处为客户端模式(client_credentials), 请求后返回凭证信息, 我们通过access_token再去访问资源服务器 使用这种授权类型,会向token 。
code 200
概要
示例地址:https://github.com/fhcodegit/IdentityServer4.Samples IdentityServer4叙述:https://www.cnblogs.com/yyfh/p/11590383.html
原文出处:https://www.cnblogs.com/yyfh/p/11595658.html
asp.net-mvc – 与SmtpClient.UseDefaultCredentials属性混淆
我已经使用以下设置配置了我的Web.Config文件:
<system.net> <mailSettings> <smtp deliveryMethod="Network"> <network enableSsl="true" defaultCredentials="false" host="smtp.gmail.com" port="587" userName="xxMyUserNamexx@gmail.com" password="xxMyPasswordxx" /> </smtp> </mailSettings> </system.net>
使用SmtpClient并发送电子邮件的方法如下所示:
public void SendMail(string fromdisplayName,string fromEmailAddress,string toEmailAddress,string subject,string body) { MailAddress from = new MailAddress(fromEmailAddress,fromdisplayName); MailAddress to = new MailAddress(toEmailAddress); MailMessage mailMessage = new MailMessage(from,to); mailMessage.Body = body; mailMessage.Subject = subject; SmtpClient client = new SmtpClient(); //client.UseDefaultCredentials = false; client.Send(mailMessage); }
以上代码按预期工作正常.有什么困扰我的是注释行client.UseDefaultCredentials = false; – 如果我要取消对该行的注释,我将收到一条异常消息,其中指出:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication required.
更重要的是,如果我将UseDefaultCredentials属性设置为true或false,我仍然会收到异常消息.避免异常消息的唯一方法是完全删除该行.
这个行为是否正常?你能解释为什么我收到异常消息吗?
解决方法
So why would me explicitly setting the property to false throw an exception?
原因是因为UseDefaultCredentials的setter将Credentials属性设置为null,如果将其设置为false,或者将其设置为CredentialCache.DefaultNetworkCredentials属性(如果设置为true). DefaultNetworkCredentials属性为defined by MSDN as:
The credentials returned by DefaultNetworkCredentials represents the authentication credentials for the current security context in which the application is running. For a client-side application,these are usually the Windows credentials (user name,password,and domain) of the user running the application. For ASP.NET applications,the default network credentials are the user credentials of the logged-in user,or the user being impersonated.
当您将UseDefaultCredentials设置为true时,它将使用您的IIS用户,并且假设您的IIS用户与您使用的任何SMTP服务器的帐户不具有与您的帐户相同的身份验证凭据.将UseDefaultCredentials设置为false将设置的凭据设置为false.所以无论哪种方式你都会得到这个错误.
以下是使用dotPeek的UseDefaultCredentials设置器:
set { if (this.InCall) { throw new InvalidOperationException( SR.GetString("SmtpInvalidOperationDuringSend")); } this.transport.Credentials = value ? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials : (ICredentialsByHost) null; }
c# – 任务/线程中的CredentialsCache.DefaultCredentials
我在我的C#4.0应用程序中进行了一些需要身份验证的webrequests.我只是使用CredentialsCache.DefaultCredentials.只要我不通过Task< T> .Factory.StartNew(…)在不同的线程/任务中运行该功能,这就很有用.然后我得到401错误.我假设凭证不会传递给子线程?
如何将凭据传递给任何子任务/线程?
解决方法
因此,鉴于上述情况,有两种方法(正式地说)执行上下文(或只是安全上下文)不是跨线程流动的.默认行为是上下文流动 – 所以必须影响它.
1)Something设置了ExecutionContext.SuppressFlow() – 您可以通过在任务中转储ExecutionContext.IsFlowSuppressed()的值来检查这一点.
2)有一个配置元素< legacyImpersonationPolicy> (conifguration-> runtime-> legacyImpersonationPolicy)默认为false.如果为false,则WindowsIdentity会在非同步点上流动.如果是真的那就不是.这与ExecutionContext流设置无关.这么真实会给你带来麻烦.您可以通过转储SecurityContext的值来检查这一点.IsWindowsIdentityFlowSuppressed()是您的任务.这也可以使用SecurityContext.SuppressFlowWindowsIdentity()以编程方式为每个线程设置.
最后,为了完整性,如果您使用的是非托管代码,则还有另一个设置< alwaysFlowImpersonationPolicy>它控制着非模仿凭据在非托管方案中的流动方式;描述的其他设置仅影响托管代码.
我们今天的关于获取Windows凭据用作CRM的ClientCredentials和System.ServiceModel.ClientCredentials的分享已经告一段落,感谢您的关注,如果您想了解更多关于10.第三方ClientCredential模式调用、asp.net core IdentityServer4 实现 Client credentials(客户端凭证)、asp.net-mvc – 与SmtpClient.UseDefaultCredentials属性混淆、c# – 任务/线程中的CredentialsCache.DefaultCredentials的相关信息,请在本站查询。
本文标签: