【问题标题】:How can i use await if i am hard coding the data instead of using query如果我对数据进行硬编码而不是使用查询,我该如何使用 await
【发布时间】:2017-11-01 04:58:50
【问题描述】:

我正在覆盖 OAuthAuthorizationServerProvider 的两种方法,但我正在对数据进行硬编码,如下所示。我如何使用等待?我可以在哪里申请,以便编译可以构建它

    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;
            }
        }
    }

【问题讨论】:

  • 您可以删除 async 关键字并为旧框架版本添加 return Task.CompletedTaskTask.FromResult(0)
  • @JSteward 可以在旧版本中编辑以上代码
  • 我不明白你的意思?
  • @JSteward 它对我有用,谢谢你的帮助
  • Task.FromResult(0) 完成了我的工作

标签: asp.net asynchronous oauth-2.0 async-await owin


【解决方案1】:

每当您编写 async 时,都必须使用 await,但使用旧方法您可以这样做。

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);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多