Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取路由数据值_C#_Asp.net Mvc - Fatal编程技术网

C# 如何获取路由数据值

C# 如何获取路由数据值,c#,asp.net-mvc,C#,Asp.net Mvc,我需要授权属性,以确保经过身份验证的用户可以在安全区域访问以管理其数据,并且只管理其数据 我想检索httpContext.User.Identity.Name,并将其与从数据库检索到的用户名相匹配 这基本上是好的(安全的)方法吗?还是可以在心里有更好的安全感 因此,我的控制器是用自定义属性装饰的 [UserAccountAuthorizeAttribute] public ActionResult Edit(string username) { return View(); } 我正在

我需要授权属性,以确保经过身份验证的用户可以在安全区域访问以管理其数据,并且只管理其数据

我想检索
httpContext.User.Identity.Name
,并将其与从数据库检索到的用户名相匹配

这基本上是好的(安全的)方法吗?还是可以在心里有更好的安全感

因此,我的控制器是用自定义属性装饰的

[UserAccountAuthorizeAttribute]
public ActionResult Edit(string username)
{
    return View();
}
我正在重写
AuthorizeAttribute
,但在调试模式下,在下面的行中得到了null值

var rd = httpContext.Request.RequestContext.RouteData;
var username = rd.Values["username"]; // null

有什么问题吗?

您不应该将用户名放在查询字符串中

如果您使用的是内置的ASP.NET成员资格提供程序,您的操作将如下所示

    [Authorize]
    public ActionResult Edit()
    {
        var userId = (Guid)Membership.GetUser().ProviderUserKey;

        var someService = new MyService();
        var someData = someService.GetSomeDataByUserId(userId);
        //...
    }