在ASP.NET5中获取AccessTokenaccess_token是在WebAPI登录调用中获取的。到目前为止,通过谷歌搜索,我为startup.cs创建了以下代码:app.UseOAuthBearerAuthentication(options=>{options.AutomaticAuthentication=true;options.Audience="http://localhost:62100/";options.Authority="http://localhost:62100/";});我的客户端是:varlogin=function(){varurl="http://localhost:62100/";vardata=$("#userData").serialize();数据=数据+"&grant_type=密码";$.post(url,data).success(saveAccessToken).always(showResponse);返回假;};我需要使用UseOpenIdConnectServer吗?如果是这样,我如何使用SigningCredentials以便获得令牌(例如MVC5ApplicationOAuthProvider)?请注意,我的网站是简单的演示HTTP站点,不需要任何SSL。我需要使用UseOpenIdConnectServer吗?使用AspNet.Security.OpenIdConnect.Server不是“必需的”。您当然可以自由选择其他服务器(如IdentityServer)或自定义解决方案。作为aspnet-contrib的主要开发者,我不是很客观,所以我绝对推荐使用app.UseOpenIdConnectServer()。如果是这样,我如何使用SigningCredentials以便获得令牌(例如MVC5ApplicationOAuthProvider)?实施密码并使用默认令牌类型时,不需要注册签名密钥/证书。下面是如何开始的:ASP.NETCore1.x:Startup.cspublicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddAuthentication();}publicvoidConfigure(IApplicationBuilderapp){//添加一个新的中间件来验证由OIDC服务器发布的//加密访问令牌。app.UseOAuthValidation();//添加一个新的中间件发行令牌。app.UseOpenIdConnectServer(options=>{options.TokenEndpointPath="/connect/token";//覆盖OnValidateTokenRequest以跳过客户端身份验证。options.Provider.OnValidateTokenRequest=context=>{//拒绝不使用的令牌请求//grant_type=passwordorgrant_type=refresh_token.if(!context.Request.IsPasswordGrantType()&&!context.Request.IsRefreshTokenGrantType()){context.Reject(error:OpenIdConnectConstants.Errors.UnsupportedGrantType,description:"Onlygrant_type=password和refresh_token"+"此服务器接受请求。");returnTask.F只读存储器结果(0);}//因为只有一个应用程序并且因为它是一个公共客户端//(即不能将其凭据保密的客户端),//调用Skip()以通知服务器请求应该被//接受而不强制执行客户端身份验证。上下文。跳过();返回Task.FromResult(0);};//覆盖OnHandleTokenRequest以支持//grant_type=password令牌请求。options.Provider.OnHandleTokenRequest=context=>{//仅处理grant_type=password令牌请求并让//OpenIDConnect服务器中间件处理其他授权类型。if(context.Request.IsPasswordGrantType()){//在这里进行凭证验证。//注意:您可以使用消息调用Reject()//以指示身份验证失败。varidentity=newClaimsIdentity(context.Options.AuthenticationScheme);identity.AddClaim(OpenIdConnectConstants.Claims.Subject,"[唯一标识]");//默认情况下,声明不会在访问和身份令牌中序列化//ns。//使用带“目的地”参数的重载//确保您的声明//正确插入到适当的标记中。identity.AddClaim("urn:customclaim","value",OpenIdConnectConstants.Destinations.AccessToken,OpenIdConnectConstants.Destinations.IdentityToken);varticket=newAuthenticationTicket(newClaimsPrincipal(identity),newAuthenticationProperties(),context.Options.AuthenticationScheme);//使用要授予的范围列表调用SetScopes//(指定offline_access以发出刷新令牌)。ticket.SetScopes("profile","offline_access");上下文。验证(票);}返回Task.FromResult(0);};});}}的.csprojASP.NETCore2.x:Startup.cspublicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddAuthentication()//添加一个新的中间件来验证由OIDC颁发的//访问令牌服务器。.AddOAuthValidation()//添加一个新的中间件issuing令牌。.AddOpenIdConnectServer(options=>{options.TokenEndpointPath="/connect/token";//覆盖OnValidateTokenRequest以跳过客户端身份验证。options.Provider.OnValidateTokenRequest=context=>{//拒绝不使用的令牌请求//grant_type=passwordorgrant_type=refresh_token.if(!context.Request.IsPasswordGrantType()&&!context.Request.IsRefreshTokenGrantType()){context.Reject(error:OpenIdConnectConstants.Errors.UnsupportedGrantType,description:"只有grant_type=password和refresh_token"+"此服务器接受请求。");returnTask.CompletedTask;}//因为只有一个应用程序并且因为它是一个公共客户端//(即不能将其凭证保密的客户端),//调用Skip()通知服务器应该在不强制执行客户端身份验证的情况下//接受请求。context.Skip();returnTask.CompletedTask;};//覆盖OnHandleTokenRequest以支持//grant_type=pas剑令牌请求。options.Provider.OnHandleTokenRequest=context=>{//仅处理grant_type=password令牌请求并让//OpenIDConnect服务器中间件处理其他授权类型。if(context.Request.IsPasswordGrantType()){//在这里进行凭证验证。//注意:您可以使用消息调用Reject()//以指示身份验证失败。varidentity=newClaimsIdentity(context.Scheme.Name);identity.AddClaim(OpenIdConnectConstants.Claims.Subject,"[唯一标识]");//默认情况下,声明不会在访问和身份令牌中序列化。//使用带“目的地”参数的重载//确保您的声明//正确插入到适当的标记中。identity.AddClaim("urn:customclaim","value",OpenIdConnectConstants.Destinations.AccessToken,OpenIdConnectConstants.Destinations.IdentityToken);varticket=newAuthenticationTicket(newClaimsPrincipal(identity),newAuthenticationProperties(),context.Scheme.Name);//使用要授予的范围列表调用SetScopes//(指定offline_access以发出刷新令牌)。票。SetScopes("配置文件","offline_access");上下文。验证(票);}返回任务.CompletedTask;};});您还可以阅读这篇博文,其中解释了如何实现资源所有者密码授权:http://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-implementing-the-ResourceOwnerPasswordCredentialIssuance/以上是C#学习教程:在ASP.NET5中获取访问令牌共享如果对你有用,需要了解更多C#学习教程,希望你会更加关注它。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
