vb.net 使用服务堆栈为客户端身份验证创建服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15511451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:52:12  来源:igfitidea点击:

Creating a service for client authentication with servicestack?

c#asp.netvb.netwcfservicestack

提问by user1632018

I have a couple of applications (mobile and desktop) that I need a simple webservice created for authentication and to post information back to the clients.

我有几个应用程序(移动和桌面),我需要为身份验证创建一个简单的 Web 服务并将信息发送回客户端。

After having man problems trying to figure out how to create a membership database or even find a previous one to check against with the WCF service I am using,I have stumbled upon service stack. So I have a couple of questions.

在尝试弄清楚如何创建成员数据库或什至找到前一个与我正在使用的 WCF 服务进行核对的问题之后我偶然发现了服务堆栈。所以我有几个问题。

Does service stack have an out of the box database and provider so that I can simply add authentication for the clients, and have it create the database itself. So I do not have to create it from scratch.

服务堆栈是否有开箱即用的数据库和提供程序,以便我可以简单地为客户端添加身份验证,并让它自己创建数据库。所以我不必从头开始创建它。

Is their an example of a servicestack service and database already so I can use as a foundation?

他们是否已经是服务堆栈服务和数据库的示例,以便我可以将其用作基础?

The whole WCF services thing is having me confused. Basically all I am looking for is a service that I can use to authorize a mobile app and desktop app, and maybe later on add some extra functionality to it. It would need its own db since it won't be run from an existing website, and a way for me to manage them.

整个 WCF 服务让我感到困惑。基本上,我正在寻找一种服务,我可以使用它来授权移动应用程序和桌面应用程序,并且可能稍后为其添加一些额外的功能。它需要自己的数据库,因为它不会从现有网站运行,并且需要我管理它们的方法。

With WCF it seems overly complex for the task and I haven't found any examples with a database already to use and a way to manage them. Ideally I would of liked to have a blank website set up just so I could administer the accounts and have the WCF service use the same database.

使用 WCF,任务似乎过于复杂,我还没有找到任何已经使用的数据库示例以及管理它们的方法。理想情况下,我希望设置一个空白网站,以便我可以管理帐户并让 WCF 服务使用相同的数据库。

Can this all be done easily with service stack, and could anyone point to an example for it already? If you have any tips on my current approach that would help aswell.

这一切都可以通过服务堆栈轻松完成吗,有人可以指出它的示例吗?如果您对我目前的方法有任何建议也会有所帮助。

回答by mythz

I recommend reading the Authentication and authorization wikiwhich explains the Authentication support built-into ServiceStack.

我建议阅读身份验证和授权维基,它解释了内置于 ServiceStack 的身份验证支持。

Backend Repository options

后端存储库选项

It describes all the potential backend repositories you can persist the authenticated UserData to, long-term:

它描述了您可以将经过身份验证的 UserData 长期保存到的所有潜在后端存储库:

Short-term Session / Caching providers

短期会话/缓存提供程序

As well as all the different caching options that's used for fast, short-term data-access of authenticated client sessions:

以及用于经过身份验证的客户端会话的快速、短期数据访问的所有不同缓存选项:

By default the MemoryCacheClientis used if one isn't specified.

如果未指定,则默认使用MemoryCacheClient

Example project

示例项目

You can look at the source code for the SocialBootstrap API projectwhich is deployed on http://bootstrapapi.apphb.comwhich is an example demo that showcases all of ServiceStack's supported authentication options enabled in a web application.

您可以查看部署在http://bootstrapapi.apphb.com上的SocialBootstrap API 项目的源代码,这是一个示例演示,展示了在 Web 应用程序中启用的所有 ServiceStack 支持的身份验证选项。

I'll re-post the code and documentation from the AppHost.ConfigureAuth(), since it already does a good job explaining how to configure it.

我将重新发布来自AppHost.ConfigureAuth()的代码和文档,因为它已经很好地解释了如何配置它。

The AppSettings is used by most Auth Providers to access additional information stored the Web.Config:

大多数身份验证提供程序使用 AppSettings 来访问存储在Web.Config 中的其他信息:

var appSettings = new AppSettings();

You use the AuthFeature plugin to Register all Authentication methods you want to enable for this web app:

您可以使用 AuthFeature 插件来注册要为此 Web 应用启用的所有身份验证方法:

Plugins.Add(new AuthFeature(
    () => new CustomUserSession(), //Use your own typed Custom UserSession type
    new IAuthProvider[] {
        new CredentialsAuthProvider(),              //HTML Form post of UserName/Password credentials
        new TwitterAuthProvider(appSettings),       //Sign-in with Twitter
        new FacebookAuthProvider(appSettings),      //Sign-in with Facebook
        new DigestAuthProvider(appSettings),        //Sign-in with Digest Auth
        new BasicAuthProvider(),                    //Sign-in with Basic Auth
        new GoogleOpenIdOAuthProvider(appSettings), //Sign-in with Google OpenId
        new YahooOpenIdOAuthProvider(appSettings),  //Sign-in with Yahoo OpenId
        new OpenIdOAuthProvider(appSettings),       //Sign-in with Custom OpenId
    }));

ServiceStack allows you to specify your own typed CustomUserSessionwhich is what it will use to persist the UserAuth data into the Session.

ServiceStack 允许您指定自己的类型CustomUserSession,它将用于将 UserAuth 数据持久化到会话中。

If you want to enable Registration services for new users so they can register and login with their supplied credentials:

如果您想为新用户启用注册服务,以便他们可以使用提供的凭据进行注册和登录:

Plugins.Add(new RegistrationFeature());

You can optionally override the default registration validation with your own custom implementation:

您可以选择使用自己的自定义实现覆盖默认注册验证:

//container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>();

If you are using an OrmLite RDBMS backend repository you need to register a DB Factory, in this case it's configured to access the UserAuth SQL Server DB:

如果您使用的是 OrmLite RDBMS 后端存储库,您需要注册一个数据库工厂,在这种情况下,它被配置为访问 UserAuth SQL Server 数据库:

var connStr = appSettings.Get("SQLSERVER_CONNECTION_STRING", //AppHarbor or Local connection string
    ConfigUtils.GetConnectionString("UserAuth"));
container.Register<IDbConnectionFactory>(
    new OrmLiteConnectionFactory(connStr, //ConnectionString in Web.Config
        SqlServerOrmLiteDialectProvider.Instance) {
            ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
        });

The above ConnectionFilter is optional, but allows you to profile the DB queries with ServiceStack's built-in Mini Profiler.

上述 ConnectionFilter 是可选的,但允许您使用 ServiceStack 的内置 Mini Profiler 分析数据库查询。

Now that you've registered your RDBMS connection above, you can hook it up so it becomes the IUserAuthRepositoryfor the Authentication Feature:

现在您已经在上面注册了您的 RDBMS 连接,您可以将它连接起来,使其成为IUserAuthRepository身份验证功能:

//Use OrmLite DB Connection to persist the UserAuth and AuthProvider info
container.Register<IUserAuthRepository>(c =>
    new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>())); 

If you use the OrmLiteAuthRepository, it can automatically create the backend User Auth tables required by the AuthFeature:

如果您使用OrmLiteAuthRepository,它可以自动创建 AuthFeature 所需的后端用户身份验证表:

//Drop and re-create all Auth and registration tables
var authRepo = (OrmLiteAuthRepository)container.Resolve<IUserAuthRepository>();
if (appSettings.Get("RecreateAuthTables", false))
    authRepo.DropAndReCreateTables(); 
else
    authRepo.CreateMissingTables(); //Create only the missing tables