简体   繁体   English

将 .NET 查询字符串绑定到属性

[英]Bind .NET query string to property

I am working with Microsoft's Verifiable Credentials sample: https://github.com/Azure-Samples/active-directory-verifiable-credentials-dotnet/tree/main/1-asp-net-core-api-idtokenhint我正在使用 Microsoft 的可验证凭据示例: https://github.com/Azure-Samples/active-directory-verifiable-credentials-dotnet/tree/main/1-asp-net-core-api-idtokenhint

Rather than use fixed contact values as per the test (see IssuerController.cs), I want to use query params from a form I have added to the Index page (eg an input called "firstname").我不想根据测试使用固定的联系人值(请参阅 IssuerController.cs),我想使用我添加到索引页面的表单中的查询参数(例如,名为“名字”的输入)。 I can see the params in the results page URL, however I cannot find a way to set these correctly.我可以在结果页面 URL 中看到参数,但是我找不到正确设置这些参数的方法。

I've tried a few methods in IssuerController.cs.我在 IssuerController.cs 中尝试了一些方法。 First by amending the static value to a query request:首先通过将 static 值修改为查询请求:

payload["issuance"]["claims"]["given_name"] = HttpContext.Request.Query["firstname"].ToString();

I've also tried setting a property:我也试过设置一个属性:

[FromQuery(Name = "firstname")]
public string firstname { get; set; }

And then adding the property to the payload:然后将属性添加到有效负载:

payload["issuance"]["claims"]["given_name"] = firstname;

Sadly all of this results in the same error:可悲的是,所有这些都会导致相同的错误:

"Missing provided claims in issuance: [given_name]","target":"issuance.claims"

For reference, the method signature is作为参考,方法签名是

public async Task<ActionResult> IssuanceRequest()

As you may gather, I am not savvy with .NET.正如你所知道的,我对 .NET 并不精通。

What is the correct way to do this please?请问这样做的正确方法是什么? Thanks谢谢

Right, I'm sure there are other ways to do this but we got it working in the end by amending the IssuanceRequest method in Issuer.cshtml是的,我确信还有其他方法可以做到这一点,但我们最终通过修改 Issuer.cshtml 中的 IssuanceRequest 方法使其工作

fetch('/api/issuer/issuance-request?firstName=@HttpContext.Request.Query["firstName"].ToString()&lastName=@HttpContext.Request.Query["lastName"].ToString()&email=@HttpContext.Request.Query["email"].ToString()&phone=@HttpContext.Request.Query["phone"].ToString()')

Thanks for the help anyway无论如何感谢您的帮助

If I understand correctly, you just can add a parameter to you action method like this:如果我理解正确,您可以像这样向您的操作方法添加一个参数:

public async Task<ActionResult> IssuanceRequest(string firstname)

Or add a FromQuery attribute if you want to specify a source of a value, and name of query parameter (optional):或者,如果要指定值的来源和查询参数的名称(可选),请添加 FromQuery 属性:

public async Task<ActionResult> IssuanceRequest([FromQuery(Name = "firstname"]string firstname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM