我找到了自己的问题答案。在开始之前,我向您展示了我定义客户机的代码。
public static IEnumerable<client>getclients())
{
//客户端凭据客户端
返回新列表<客户端>
{
//资源所有者密码授予客户端
新客户端
{
clientid=“ro.angular”,
allowedgranttypes=granttypes.resourceownerpassword,
客户机密=
{
新秘密(“secret”.sha256())
},请
允许范围={
IdentityServerConstants.StandardScopes.OpenID,
IdentityServerConstants.StandardScopes.Profile,配置文件,
IdentityServerConstants.StandardScopes.email,
IdentityServerConstants.StandardScopes.Address,
“API1”
}
}
}(二)
}
现在,我在登录方法中所做的就是使用tokenclient类来请求令牌。要创建实例,需要传入令牌端点地址、客户端ID和机密。
接下来我要使用使用密码grant请求令牌to allows a client to send username and password to the token service and get an access token back that representative that user.
这是我需要修改的登录代码:
public async task<iactionresult>登录([frombody]loginviewmodel model)
{
var disco=await discovercyclient.getasync(“http://localhost:52718”);
如果(迪斯科·伊瑟罗)
{
返回BAD请求(发现错误);
}
var tokenclient=新tokenclient(disco.tokenpoint,“ro.angular”,“secret”);
var tokenresponse=await tokenclient.requestResourceOwnerPasswordAsync(model.username,model.password,“api1 openid”);
if(tokenResponse.isError)
{
返回badRequest(tokenResponse.error);
}
如果(!模型状态.isvalid)
{
返回BADREQUEST(ModelState);
}
var user=_usermanager.findbynameasync(model.username);
var result=await _usermanager.findbynameasync(model.username);
如果(结果!=空&wait UserManager.checkPasswordAsync(result,model.password))
{
返回OK(new profileviewmodel(result,tokenResponse));
}
返回badrequest(“用户名或密码无效”);
}
另外,我还修改了profileviewModel类,并添加了两个新的标记&expiry:。
公共类profileviewModel
{
公共字符串ID_get;set;
公共字符串firstname_get;set;
公共字符串姓氏get;set;
公共字符串电子邮件get;set;
公共字符串标记get;set;
public int expiry_get;set;
公共配置文件视图模型()
{
}
public profileviewmodel(applicationuser用户,tokenResponse utoken=null)
{
id=user.id;
firstname=user.firstname;
lastname=user.lastname;
email=user.email;
token=utoken.accesstoken;
expiry=utoken.expiresin;
}
公共静态IEnumerable<ProfileViewModel>GetUserProfiles(IEnumerable<ApplicationUser>用户)
{
var profiles=新列表<profileviewModel>
foreach(用户中的applicationuser用户)
{
profiles.add(new profileviewmodel(user));
}
返回配置文件;
}
}
这是我的欲望输出。希望这个答案能帮助其他人。

现在,我在登录方法中所做的就是使用tokenclient类来请求令牌。要创建实例,需要传入令牌端点地址、客户端ID和机密。
接下来我用Requesting a token using the password grant允许客户机向令牌服务发送用户名和密码,并返回代表该用户的访问令牌。
这是我需要修改的登录代码:
public async Task<IActionResult> Signin([FromBody]LoginViewModel model)
{
var disco = await DiscoveryClient.GetAsync("http://localhost:52718");
if (disco.IsError)
{
return BadRequest(disco.Error);
}
var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.angular", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(model.UserName, model.Password, "api1 openid");
if (tokenResponse.IsError)
{
return BadRequest(tokenResponse.Error);
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = _userManager.FindByNameAsync(model.UserName);
var result = await _userManager.FindByNameAsync(model.UserName);
if (result != null && await _userManager.CheckPasswordAsync(result, model.Password))
{
return Ok(new ProfileViewModel(result, tokenResponse));
}
return BadRequest("Invalid username or password.");
}
此外,我还修改profileviewModel类并添加两个新标记&expiry:
public class ProfileViewModel
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Token { get; set; }
public int Expiry { get; set; }
public ProfileViewModel()
{
}
public ProfileViewModel(ApplicationUser user, TokenResponse UToken = null)
{
Id = user.Id;
FirstName = user.FirstName;
LastName = user.LastName;
Email = user.Email;
Token = UToken.AccessToken;
Expiry = UToken.ExpiresIn;
}
public static IEnumerable<ProfileViewModel> GetUserProfiles(IEnumerable<ApplicationUser> users)
{
var profiles = new List<ProfileViewModel> { };
foreach (ApplicationUser user in users)
{
profiles.Add(new ProfileViewModel(user));
}
return profiles;
}
}
这是我的欲望输出。希望这个答案能帮助别人。
