在服务器端Blazor中使用SignInManager [英] Using SignInManager in server-side Blazor

查看:303
本文介绍了在服务器端Blazor中使用SignInManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在没有HTTPContext的情况下使用SignInManager?我正在制作Blazor服务器端应用程序,我需要使最终用户使用SignInManagerPasswordSignInAsync()方法登录.

Is it possible to use SignInManager without having some HTTPContext ? I'm making a Blazor server-side app and I need to make end-users signed in using PasswordSignInAsync() method of SignInManager.

如果还有其他方法可以对Cookie或其他方法进行处理,只要它们是干净的"方法,我也会接受.

If there is other ways to do it with cookies or something else, I'll take it too, as long they are "clean" methods.

如果我还能获得有关如何为Startup.cs解决方案进行配置的说明,那将是完美的.

If I could get also an explanation on how to configure for Startup.cs the solution, it will be perfect.

推荐答案

我强烈建议您在应用中使用身份验证系统. 您不应该尝试创建任何身份验证系统.这是免费的,并且在几分钟之内即可完成设置.不要浪费时间去做一些复杂的事情.相反,了解什么是Blazor授权组件以及如何在应用程序中使用它们.他们都是伟大的.

I'd strongly suggest that you use the Identity authentication system in your app. You should not try to create any authentication system instead. This is something free, and is set up within a couple of minutes. Don't waste your time to do something so complicated. Instead learn what are the Blazor Authorization components and how to use them in your application. They are great.

注意:Blazor服务器与其客户端之间的通信是通过SignaleR完成的. HttpContext在大多数情况下不可用.不要尝试使用HttpContext.实际上,您不能这样做,因为无论何时尝试访问它,它都是null.

Note: The communication between Blazor Server and its client-side is done through SignaleR. HttpContext is not available most of the time. Do not try to use the HttpContext. Actually, you can't because whenever you try to access it, it is null.

以下内容描述了如何使用身份UI创建Blazor Server应用程序:

The following describe how you can create a Blazor Server App with the Identity UI:

  • 开始创建Blazor应用

  • Start creating a Blazor App

在名为创建新Blazor应用程序"的窗口中,执行以下操作:

In the window titled Create a new Blazor app do this:

  1. 选择Blazor服务器应用程序
  2. 窗口的右侧是身份验证"标题下带有更改"文本的链接.点按链接,然后选择个人用户帐户".按确定...
  3. 点击创建"按钮

Visual Studio为您创建了具有身份UI的Blazor Server应用程序,以对您的用户进行身份验证.请注意,Identity UI实际上是与Razor Pages和MVC一起使用的Razor Pages Identity UI.如果您需要进行一些更改,则可以从该系统中架设一个或多个项目.转到解决方案资源管理器,并验证Visual Studio是否已添加两个名为Areas和Data的文件夹.她还为您的Startup类配置了必要的服务,以管理Identity UI.现在,您想要创建一个存储用户名,角色,声明等的数据库.要创建数据库,您应该使用 migrations . 此处是链接告诉您如何运行为您完成任务的命令.现在,您可以运行您的应用程序,注册到网站,登录,注销等.

Visual Studio has created for you a Blazor Server App with Identity UI to authenticate your users. Note that the Identity UI is actually the Razor Pages Identity UI used with Razor Pages and MVC. You may scaffold one or more items from this system if you need to make some changes. Go to solution explorer and verify that Visual Studio has added two folders named Areas and Data. She also has configured your Startup class with the necessary services to mange the Identity UI. What you want now is to create the database where user names, roles, claims, etc. are stored. To create the database you should use migrations. Here is a link telling you how to run the commands that do the job for you. Now you can run your app, register to the web site, login, logout, etc.

上帝的运气.

这篇关于在服务器端Blazor中使用SignInManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆