ServiceStack + Swagger - CredentialsAuthProvider

时间:2015-07-28 14:29:03

标签: authentication servicestack restful-authentication swagger-ui

如何在实现自定义身份验证提供程序时使用Swagger UI对ServiceStack进行身份验证(CredentialsAuthProvider)。

对于基本身份验证提供程序,我只会发送带有base64编码用户的密码,但是这个怎么样?

1 个答案:

答案 0 :(得分:2)

从现在available on MyGet v4.0.43 + 开始,ServiceStack的Swagger API Feature支持添加UserNamePassword,并BasicAuthProvider 3}}为每个允许访问经过身份验证的服务的Swagger请求:

HTTP Basic Auth Headers

这使用AuthFeature是通过Swagger UI Basic Auth启用身份验证,可以在注册Plugins.Add(new AuthFeature(..., new IAuthProvider[] { new BasicAuthProvider(appSettings), ... }); 时启用身份验证:

CredentialsAuthProvider

如果您只启用了/auth/credentials=Username=xxx&Password=xxx ,则需要首先在Swagger之外进行身份验证,例如手动调用URL:

    set userID to "username"
    set pwd to "password"

    tell application "Safari"
    activate

    make new document with properties {URL:"http://example.com"}  (*or:         
    open location "example.com"*)

    tell application "System Events"
    delay 4
    keystroke tab

    keystroke userID
    delay 1
    keystroke tab

    keystroke pwd
    delay 1
    keystroke return
    delay 3

    tell application "Safari"  
   (*or: tell application "Safari" to tell active tab of window 1*)

    do JavaScript "$( 'apps-button .switcher-button switcher-button-   
    left :equ(0)').click()" in front document
    (*or: do JavaScript "document.getElementByID('apps-  
     button').click()"*)

    end tell
    end tell
    end tell

如果成功,您可以使用Swagger UI访问经过身份验证的服务。