MVC ASP.NET – 手动授权某人并通过表单身份验证保留授权
发布时间:2020-05-24 09:52:08 所属栏目:asp.Net 来源:互联网
导读:我想要ASP.NET中的表单身份验证的好处.我希望它能继续授权我这样,但是我的情况有一点不同;我想对一个简单的Web服务(特别是由客户端提供)进行身份验证. 我有我的代码来查看Web位置并查看它们是否应该被授权,但是如何在ASP.NET中设置cookie [?]或授权标志,他们
|
我想要ASP.NET中的表单身份验证的好处.我希望它能继续授权我这样,但是我的情况有一点不同;我想对一个简单的Web服务(特别是由客户端提供)进行身份验证. 我有我的代码来查看Web位置并查看它们是否应该被授权,但是如何在ASP.NET中设置cookie [?]或授权标志,他们知道当前用户已获得授权. 基本上… if (HttpContext.Current.User.Identity.IsAuthenticated) // we're all good //Other wise... bool success = CheckClientsWebService(string username,string password); if (success) // Somehow tell .NET that they're authorized *注意:这是一个相当简单的服务,不处理组或角色.只需检查用户是否可以查看该站点. 解决方法在表单中,身份验证不能证明您是谁在表单身份验证cookie中.考虑到这一点,您无法在自定义登录表单中创建票证而无需创建自定义提供程序?我绝对认为你可以.进行快速测试并创建表单身份验证票证,并查看开箱即用的成员资格提供程序是否认为用户已通过身份验证.我很好奇 – 所以这里有一些代码.. 模型 public class SignInViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
调节器 public class SignInController : Controller
{
public ActionResult Index()
{
var model = new SignInViewModel {};
return View(model);
}
[HttpPost]
public ActionResult Index(SignInViewModel model)
{
if (model.Username == "Fred" && model.Password == "Mertz")
{
FormsAuthentication.SetAuthCookie(model.Username,false);
return RedirectToAction("Secure");
}
return View(model);
}
[Authorize]
public ActionResult Secure(SignInViewModel model)
{
return View();
}
[Authorize]
public ActionResult Logout(SignInViewModel model)
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}
Index.cshtml @using (Html.BeginForm()) {
<fieldset>
<legend>SignInViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Username)
@Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="Login" />
</p>
</fieldset>
}
Secure.cshtml <h2>Secure</h2>
@Html.ActionLink("Logout","Logout") (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC网格控件比较
- asp.net-mvc – 为多语言ASP.NET MVC Web应用程序设置Curre
- asp.net-mvc – 在ASP.NET MVC中对ViewModels进行验证
- asp-classic – 经典ASP的好IDE?
- asp.net-mvc – 如何在MVC Unobtrusive Validation中验证Da
- asp.net-mvc – 使用一组复杂数据类型调用UpdateModel会重置
- asp-classic – 使用SMTP身份验证的经典ASP发送电子邮件
- asp.net-mvc – MVC DB首先修复显示名称
- asp.net – 如何在web.config中读取会话状态信息
- asp.net – 迁移匿名配置文件的最佳方式
推荐文章
站长推荐
热点阅读
