我正试图通过.net maui对安卓系统进行认证,我已经设置了清单以正确地弹出AAD,并且我可以登录并从天蓝色获取我的id,问题是标记没有使用blazor授权。
我在github https://github.com/dotnet/maui/issues/2529上遵循了这个问题的解决方案,并放置了我自己的混合身份验证状态提供程序,我有一个经过身份验证的类用户,它包含一个ClaimsPrinciple,这个用户在首次加载应用程序时被填充,我使用了一些DI来设置作用域AuthenticatedUser,但是它没有将它自己附加到身份验证StateProvider
到目前为止这是我的密码。这是在应用程序第一次启动时触发的。
var authService = new AuthService(); // most likely you will inject it in constructor, but for simplicity let's initialize it here
var result = await authService.LoginAsync(CancellationToken.None);
var token = result?.IdToken; // you can also get AccessToken if you need it
if (token != null)
{
var handler = new JwtSecurityTokenHandler();
var data = handler.ReadJwtToken(token);
var claims = data.Claims.ToList();
}
_authenticatedUser.Principal = result.ClaimsPrincipal;8月份的服务是:
private readonly IPublicClientApplication authenticationClient;
public AuthService()
{
authenticationClient = PublicClientApplicationBuilder.Create(Constants.ClientId)
//.WithB2CAuthority(Constants.AuthoritySignIn) // uncomment to support B2C
.WithRedirectUri($"msal{Constants.ClientId}://auth")
.Build();
}
public async Task<AuthenticationResult> LoginAsync(CancellationToken cancellationToken)
{
AuthenticationResult result;
try
{
result = await authenticationClient
.AcquireTokenInteractive(Constants.Scopes)
.WithAuthority("[TENNANT ID HERE]")
.WithPrompt(Prompt.ForceLogin)
#if ANDROID
.WithParentActivityOrWindow(Platform.CurrentActivity)
#endif
.ExecuteAsync(cancellationToken);
return result;
}
catch (MsalClientException)
{
return null;
}
}常量只保存客户端id,所以应用程序启动,它重定向到登录,获取令牌,得到一个JWT和声明,然后将_authencatedUser.Principle设置为这个声明,
在我的HybridStateAuthenticator中,如下所示:
public class HybridAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly Task<AuthenticationState> _authenticationState;
public HybridAuthenticationStateProvider(AuthenticatedUser user) =>
_authenticationState = Task.FromResult(new AuthenticationState(user.Principal));
public override Task<AuthenticationState> GetAuthenticationStateAsync() =>
_authenticationState;
}
public class AuthenticatedUser
{
public ClaimsPrincipal Principal { get; set; }
}我要问的是如何将这个Stateprovider附加到Maui,然后使用授权视图获取上下文标识
发布于 2022-06-06 09:35:51
我将这个github解决方案集成到我的登录逻辑https://github.com/mitchelsellers/dotnet-maui-blazor-customauthentication-sample中,这是非常有用的指导。
https://stackoverflow.com/questions/72308400
复制相似问题