Asp.net 如果我硬编码数据而不是使用查询,如何使用wait
我覆盖了OAuthAuthorizationServerProvider的两个方法,但是我正在硬编码数据,正如您在下面看到的那样。我如何使用wait?我可以在哪里应用,以便编译可以构建它Asp.net 如果我硬编码数据而不是使用查询,如何使用wait,asp.net,asynchronous,oauth-2.0,async-await,owin,Asp.net,Asynchronous,Oauth 2.0,Async Await,Owin,我覆盖了OAuthAuthorizationServerProvider的两个方法,但是我正在硬编码数据,正如您在下面看到的那样。我如何使用wait?我可以在哪里应用,以便编译可以构建它 public class MyAutorization : OAuthAuthorizationServerProvider { public override async Task ValidateClientAuthentication(OAuthValidateClient
public class MyAutorization : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
if (context.UserName == "admin" && context.Password == "admin")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
identity.AddClaim(new Claim("username", "admin"));
identity.AddClaim(new Claim(ClaimTypes.Name, "Mulla"));
context.Validated(identity);
}
else if (context.UserName == "user" && context.Password == "user")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
identity.AddClaim(new Claim("username", "user"));
identity.AddClaim(new Claim(ClaimTypes.Name, "Boland"));
context.Validated(identity);
}
else
{
context.SetError("Access Denied", "Invalid User Name And Password");
return;
}
}
}
当您编写异步时,使用wait是必要的,但是使用旧的aproach可以做到这一点
public class MyAutorization : OAuthAuthorizationServerProvider
{
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
return Task.FromResult(0);
}
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
// await Task.CompletedTask
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
if (context.UserName == "admin" && context.Password == "admin")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
identity.AddClaim(new Claim("username", "admin"));
identity.AddClaim(new Claim(ClaimTypes.Name, "Mulla"));
context.Validated(identity);
}
else if (context.UserName == "user" && context.Password == "user")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
identity.AddClaim(new Claim("username", "user"));
identity.AddClaim(new Claim(ClaimTypes.Name, "Boland"));
context.Validated(identity);
}
else
{
context.SetError("Access Denied", "Invalid User Name And Password");
}
return Task.FromResult(0);
}
}
对于较旧的框架,您可以删除
async
关键字并添加return Task.CompletedTask
或Task.FromResult(0)
versions@JSteward可以在旧版本中编辑以上代码吗?我不明白你的意思?@JSteward它对我有用,谢谢你的帮助任务。FromResult(0)完成了我的工作