添加身份验证用户界面

时间:2019-05-30 13:08:31

标签: c# asp.net asp.net-web-api swagger-ui

我有一个带有令牌,基于身份验证的C#ASP.NET Web API应用程序 身份验证的实现如下在startup.cs中

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // Enable CORS (cross origin resource sharing) for making request using browser from different domains
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            //The Path For generating the Toekn
            TokenEndpointPath = new PathString("/api/token"),
            //Setting the Token Expired Time (24 hours)
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            //MyAuthorizationServerProvider class will validate the user credentials
            Provider = new MyAuthorizationServerProvider()
        };
        //Token Generations
        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
    }
}

身份验证的定义在以下类中完成

public class MyAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        using (UserMasterRepository _repo = new UserMasterRepository())
        {
            var user = _repo.ValidateUser(context.UserName, context.Password);
            if (user == null)
            {
                context.SetError("invalid_grant", "Provided username and password is incorrect");
                return;
            }
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Role, user.UserRoles));
            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            identity.AddClaim(new Claim("Email", user.UserEmailID));
            context.Validated(identity);
        }
    }
}

我已在应用程序中添加了Swagger,它运行良好:它显示了我在webapi应用程序中实现的所有方法 除了身份验证UI 我用PostMan测试了该应用程序,在其中填写了用户名和密码后即可获得令牌 我想在SWAGGER中做同样的事情:添加一个UI,可以在填写登录名/密码后从中获得令牌

我该怎么做?

发布问题后,我发现a solution 我在 Swaggerconfig.cs 文件中实现的 但我收到此错误“ swaggerUiconfig不包含 DocumentFilter 我添加了

class AuthTokenOperation : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            swaggerDoc.paths.Add("/api/token", new PathItem
            {
                post = new Operation
                {
                    tags = new System.Collections.Generic.List<string> { "Auth" },
                    consumes = new List<string>
            {
                "application/x-www-form-urlencoded"
            },
                    parameters = new List<Parameter> {
                new Parameter
                {
                    type = "string",
                    name = "grant_type",
                    required = true,
                    @in = "formData"
                },
                new Parameter
                {
                    type = "string",
                    name = "username",
                    required = false,
                    @in = "formData"
                },
                new Parameter
                {
                    type = "string",
                    name = "password",
                    required = false,
                    @in = "formData"
                }
            }
                }
            });
        }
    }

但调用c.DocumentFilter();不被接受 似乎与DocumentFilter有版本冲突 我该如何解决?

0 个答案:

没有答案