Fir*_*ion 7 c# .net-core blazor
我已经有包含用户名、密码和角色 ID 的表,所以我不想使用 ASP.NET Membership 表。而且我只需要一个要求输入用户名和密码的简单登录页面,而不是他们预先构建的 Identity.UI 包。
但我不知道如何让 Blazor Server 为我进行基本登录。我有我认为应该在 Startup.cs 中配置的基础知识,例如:
在配置服务中:
services
.AddAuthentication(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
Run Code Online (Sandbox Code Playgroud)
在配置()中:
app.UseAuthentication();
app.UseAuthorization();
Run Code Online (Sandbox Code Playgroud)
通常我会这样称呼:
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
principal,
new AuthenticationProperties { IsPersistent = loginData.RememberMe });
Run Code Online (Sandbox Code Playgroud)
事实上,如果我从 .cshtml 页面运行它,上面的确实有效,但我希望它作为 Blazor 组件 (.razor) 工作。
我尝试注入httpContextAccessor但尝试SignInAsync从那里调用该方法导致有关已设置标头的错误。
我有一个 .razor 登录组件,我不知道该调用什么来进行实际登录。
@page "/login"
@using Microsoft.AspNetCore.Authentication.Cookies;
@using Microsoft.AspNetCore.Components;
@using System.Security.Claims;
@inject NavigationManager navigationManager
@inject DataAccess.Models.CurrentDataContext db
<h3>Login</h3>
<div>
Email:
<input type="text" @bind="@email" />
</div>
<div>
Password:
<input type="text" @bind="@password" />
</div>
<div>
<button class="btn btn-primary" @onclick="DoLogin">Log In</button>
<span class="text-danger">@loginErrorMessage</span>
</div>
@code {
string email = "";
string password = "";
string loginErrorMessage = "";
protected async Task DoLogin()
{
var emailTrimmed = email.ToLower().Trim();
var user = db.UserAccount.FirstOrDefault(u => u.EmailAddress.ToLower() == emailTrimmed);
if (user != null) //if (user != null)
{
//TODO check password
var claims = new List<Claim> {
new Claim(ClaimTypes.Name, emailTrimmed)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
//SIGN in the user
///////////////////////////
// WHAT GOES HERE? ////
///////////////////////////
//
navigationManager.NavigateTo("/fetchdata");
}
else
{
loginErrorMessage = "Error logging in.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我制作了一个版本,它基于这个人的帖子:https : //www.oqtane.org/Resources/Blog/PostId/527/exploring-authentication-in-blazor?fbclid=IwAR0rbQkY47cHHxs29HWCk0RggH7GHeLDx3kJ4v3bhgomts
老实说,我自己制作的项目很少,但主要是从上面的 oqtane 帖子中窃取了我需要的东西。
我认为他的项目基于迈克尔的上述回答。
我有一个关于 git 的项目(第一次使用 git,我提交的太多了,目前有点乱):https : //github.com/TroelsMortensen/DNP/tree/master/Blazor/JSLogin
我必须承认我并不完全确定,我在做什么。而且它还没有完成。现在没有对用户数据库或实际验证或其他任何检查。并且声明是硬编码的。但这应该很容易实现。我需要一些超级简单的 cookie 身份验证,而不需要 Identity。通过这种方法,我几乎可以自己控制一切。我有一个 javascript(同样,不是我的),它可以将发布请求发送到剃刀页面,我想。
Pages/Account/Login.razor 向 Shared/Interop.cs 发送调用,使其使用 wwwroot/interop.js 中的 javascript 调用 Pages/Account/LoginRequester.cshtml.cs 中的 post 方法。我认为。
我在 Pages/_Host.cshtml 中包含了 interop.js 以访问它
我使用 HttpContext 登录。 LoginDisplay.razor 被插入到 Shared/MainLayout.razor 中。
正如我在: 简单服务器端 Blazor Cookie 身份验证演示中介绍的那样
您可以使用 .cshtml 文件中的以下代码来登录人员:
public class LoginModel : PageModel
{
public string ReturnUrl { get; set; }
public async Task<IActionResult>
OnGetAsync(string paramUsername, string paramPassword)
{
string returnUrl = Url.Content("~/");
try
{
// Clear the existing external cookie
await HttpContext
.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
}
catch { }
// *** !!! This is where you would validate the user !!! ***
// In this example we just log the user in
// (Always log the user in for this demo)
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, paramUsername),
new Claim(ClaimTypes.Role, "Administrator"),
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
IsPersistent = true,
RedirectUri = this.Request.Host.Value
};
try
{
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
}
catch (Exception ex)
{
string error = ex.Message;
}
return LocalRedirect(returnUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
至于让用户登录 .razor 页面,不用担心。您必须执行完整的回发才能建立身份验证 cookie。.razor 页面没有“回发”,但 .cshtml 页面有。
| 归档时间: |
|
| 查看次数: |
11668 次 |
| 最近记录: |