Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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_Authentication_Cookies - Fatal编程技术网

C# 自定义用户身份验证

C# 自定义用户身份验证,c#,asp.net-mvc,authentication,cookies,C#,Asp.net Mvc,Authentication,Cookies,我想实现自定义用户身份验证。我有用户名文本框和密码文本框,还有提交表单的按钮 @using (@Html.BeginForm("Submit","Profile")) { <table> <tr> <td>Username: </td> <td>@Html.TextBox("Username")</td> </tr>

我想实现自定义用户身份验证。我有用户名文本框和密码文本框,还有提交表单的按钮

@using (@Html.BeginForm("Submit","Profile"))
{

    <table>
        <tr>
            <td>Username: </td>
            <td>@Html.TextBox("Username")</td>
        </tr>
        <tr>
            <td>Password: </td>
            <td>@Html.TextBox("Password")</td>
        </tr>
        <tr>
            <td></td>
            <td><input id="btnSubmit" type="submit" value="Submit" /></td>
        </tr>
    </table>
}

我只想检查是否没有cookie,操作方法是否不可访问。如果有一个cookie可以访问。

根据我的评论,类似这样的东西应该可以工作:

var cookie = Request.Cookie["myCookie"];

if (cookie == null)
    return new HttpUnauthorizedResult();

为什么不使用FormsAuthentication.SetAuthCookie方法设置coockie和standart[Authorize]属性?

检查请求中是否存在cookie,如果不存在,发送401响应?如何发送401响应?返回新的HttpUnauthorizedResult
var cookie = Request.Cookie["myCookie"];

if (cookie == null)
    return new HttpUnauthorizedResult();