Login registration
parent
009015ce3c
commit
0bcabbd4ff
@ -0,0 +1,54 @@
|
||||
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
|
||||
namespace Yavsc.Extensions;
|
||||
|
||||
internal static class HttpContextExtensions
|
||||
{
|
||||
public static async Task SignInAsync(this HttpContext context,
|
||||
ApplicationUser user, RoleManager<IdentityRole> roleManager,
|
||||
bool rememberMe,
|
||||
ApplicationDbContext applicationDbContext)
|
||||
{
|
||||
AuthenticationProperties props = null;
|
||||
if (AccountOptions.AllowRememberLogin && rememberMe)
|
||||
{
|
||||
props = new AuthenticationProperties
|
||||
{
|
||||
IsPersistent = true,
|
||||
ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration),
|
||||
// Parameters =
|
||||
};
|
||||
};
|
||||
|
||||
// roles
|
||||
var roles = applicationDbContext.UserRoles.Where(r => r.UserId == user.Id).ToArray();
|
||||
|
||||
// issue authentication cookie with subject ID and username
|
||||
|
||||
List<Claim> additionalClaims = new List<Claim>();
|
||||
|
||||
foreach (var role in roles)
|
||||
{
|
||||
var idRole = await roleManager.Roles.SingleOrDefaultAsync(i => i.Id == role.RoleId);
|
||||
if (idRole != null)
|
||||
{
|
||||
additionalClaims.Add(new Claim(ClaimTypes.Role, idRole.Name));
|
||||
}
|
||||
}
|
||||
additionalClaims.Add(new Claim(ClaimTypes.Name, user.UserName));
|
||||
var isUser = new IdentityServerUser(user.Id)
|
||||
{
|
||||
DisplayName = user.UserName,
|
||||
AdditionalClaims = additionalClaims.ToArray()
|
||||
};
|
||||
|
||||
await context.SignInAsync(isUser, props);
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Yavsc.Attributes.Validation;
|
||||
|
||||
namespace Yavsc.ViewModels.Manage
|
||||
{
|
||||
public class ChangeUserNameViewModel
|
||||
{
|
||||
[YaRequired]
|
||||
[Display(Name = "New user name"),RegularExpression(Constants.UserNameRegExp)]
|
||||
public string NewUserName { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Yavsc.Attributes.Validation;
|
||||
|
||||
namespace Yavsc.ViewModels.Manage
|
||||
{
|
||||
public class SetUserNameViewModel
|
||||
{
|
||||
[Required]
|
||||
[Display(Name = "User name"),RegularExpression(Constants.UserNameRegExp)]
|
||||
public string UserName { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
@model RegisterModel
|
||||
|
||||
<partial name="_ValidationSummary" />
|
||||
<form method="post">
|
||||
@Html.EditorForModel()
|
||||
<button class="btn btn-primary" name="button"
|
||||
value="Register">Register</button>
|
||||
</form>
|
||||
@ -0,0 +1,32 @@
|
||||
@using System.Diagnostics
|
||||
|
||||
@{
|
||||
var version = FileVersionInfo.GetVersionInfo(typeof(IdentityServer4.Hosting.IdentityServerMiddleware).Assembly.Location).ProductVersion.Split('+').First();
|
||||
}
|
||||
|
||||
<div class="welcome-page">
|
||||
<h1>
|
||||
<img src="~/icon.jpg">
|
||||
Welcome to IdentityServer4
|
||||
<small class="text-muted">(version @version)</small>
|
||||
</h1>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
IdentityServer publishes a
|
||||
<a href="~/.well-known/openid-configuration">discovery document</a>
|
||||
where you can find metadata and links to all the endpoints, key material, etc.
|
||||
</li>
|
||||
<li>
|
||||
Click <a href="~/diagnostics">here</a> to see the claims for your current session.
|
||||
</li>
|
||||
<li>
|
||||
Click <a href="~/grants">here</a> to manage your stored grants.
|
||||
</li>
|
||||
<li>
|
||||
Here are links to the
|
||||
<a href="https://github.com/identityserver/IdentityServer4">source code repository</a>,
|
||||
and <a href="https://github.com/IdentityServer/IdentityServer4/tree/main/samples">ready to use samples</a>.
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
@ -1,19 +1,19 @@
|
||||
@model ChangeUserNameViewModel
|
||||
@model SetUserNameViewModel
|
||||
@{
|
||||
ViewData["Title"] = "Changer de nom d'utilisateur";
|
||||
}
|
||||
|
||||
<h2>@ViewData["Title"].</h2>
|
||||
|
||||
<form asp-controller="Manage" asp-action="ChangeUserName" method="post" class="form-horizontal" role="form">
|
||||
<form asp-controller="Manage" asp-action="SetUserName" method="post" class="form-horizontal" role="form">
|
||||
<h4>Change user name form</h4>
|
||||
<hr />
|
||||
<div asp-validation-summary="All" class="text-danger"></div>
|
||||
<div class="form-group">
|
||||
<label asp-for="NewUserName" class="col-md-2 control-label"></label>
|
||||
<label asp-for="UserName" class="col-md-2 control-label"></label>
|
||||
<div class="col-md-10">
|
||||
<input asp-for="NewUserName" class="form-control" />
|
||||
<span asp-validation-for="NewUserName" class="text-danger"></span>
|
||||
<input asp-for="UserName" class="form-control" />
|
||||
<span asp-validation-for="UserName" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@ -1,12 +1,14 @@
|
||||
@model ApplicationUser
|
||||
@{
|
||||
var avuri = "/Avatars/"+Model.UserName+".s.png";
|
||||
var userPosted = Model.Posts!=null && Model.Posts.Count()>1;
|
||||
}
|
||||
<div class="userinfo">
|
||||
<h3>
|
||||
<img src="@avuri" asp-append-version="true" class="smalltofhol" />
|
||||
</h3>
|
||||
@if (Model.Posts!=null && Model.Posts.Count()>1) { <a asp-controller="Blogspot" asp-action="UserPosts"
|
||||
asp-route-id="@Model.UserName" class="btn btn-primary">index de ses articles</a>
|
||||
@if (userPosted) { <a asp-controller="Blogspot" asp-action="UserPosts"
|
||||
asp-route-id="@Model.UserName" class="btn btn-primary">
|
||||
<img src="@avuri" asp-append-version="true" class="smalltofhol" alt="" title="@Model.UserName"/>
|
||||
</a>
|
||||
}else {
|
||||
Html.LabelFor(m=>m.UserName);
|
||||
}
|
||||
</div>
|
||||
|
||||
Loading…
Reference in New Issue