Sag*_*r K 5 .net c# openid oauth-2.0 .net-core
我的客户端应用程序(即网络聊天)使用 openid 身份验证,生成的访问令牌也传递给机器人以在调用图形 api 时使用。
启动代码:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
//.AddAzureAd(options => this.Configuration.Bind("Authentication:AzureAd", options))
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ClientId = "cid";
options.ClientSecret = "csercet";
options.Authority = string.Format(azureAdConfig.Instance, azureAdConfig.TenantId);
options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
options.Resource = "https://graph.microsoft.com/";
options.Events = new AuthEvents(azureAdConfig);
});
Run Code Online (Sandbox Code Playgroud)
一旦在 authevents 中收到授权代码,就会生成访问令牌:
public override async Task AuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
var principal = context.Principal;
var request = context.HttpContext.Request;
var currentUri = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path);
var tokenService = (ITokenService)context.HttpContext.RequestServices.GetService(typeof(ITokenService));
try
{
var x = await tokenService.RequestTokenAsync(principal, context.ProtocolMessage.Code, currentUri, "https://graph.microsoft.com/")
.ConfigureAwait(false);
context.HandleCodeRedemption(x.AccessToken, x.IdToken);
}
catch (System.Exception ex)
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
result = await authContext.AcquireTokenAsync(resource, "clientid", new Uri(redirectURI), new PlatformParameters(PromptBehavior.RefreshSession, null));
Run Code Online (Sandbox Code Playgroud)
参数类:
#region Assembly Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// C:\Users\v-sagkul\.nuget\packages\microsoft.identitymodel.clients.activedirectory\5.2.8\lib\netstandard1.3\Microsoft.IdentityModel.Clients.ActiveDirectory.dll
#endregion
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Extensibility;
namespace Microsoft.IdentityModel.Clients.ActiveDirectory
{
//
// Summary:
// Additional parameters used in acquiring user's authorization.
public class PlatformParameters : IPlatformParameters
{
//
// Summary:
// Constructor that allows extends to configure their own web ui. Not implemented
// on Android, iOS and UWP.
//
// Parameters:
// promptBehavior:
// Controls the prompt that is displayed on web ui. Default is Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior.SelectAccount.
//
// customWebUi:
// Custom implementation of the web ui
//
// Remarks:
// This object is platform specific and should not be constructed from NetStandard
// (shared) assemblies.
public PlatformParameters(PromptBehavior promptBehavior, ICustomWebUi customWebUi);
//
// Summary:
// Gets the configured prompt behavior
public PromptBehavior PromptBehavior { get; }
//
// Summary:
// Extension method enabling ADAK.NET extenders for public client applications to
// set a custom web ui that will let the user sign-in with Azure AD, present consent
// if needed, and get back the authorization code.
public ICustomWebUi CustomWebUi { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
985 次 |
| 最近记录: |