con*_*att 10 asp.net forms-authentication asp.net-mvc-3
我想要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
Run Code Online (Sandbox Code Playgroud)
*注意:这是一个相当简单的服务,不处理组或角色.只需检查用户是否可以查看该网站.
在表单中,身份验证不能证明您在表单身份验证cookie中的身份.考虑到这一点,您无法在自定义登录表单中创建票证而无需创建自定义提供程序吗?我绝对认为你可以.进行快速测试并创建表单身份验证票证,并查看开箱即用的成员资格提供程序是否认为用户已通过身份验证.
我很好奇 - 所以这里有一些代码..
模型
public class SignInViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调节器
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");
}
Run Code Online (Sandbox Code Playgroud)
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>
}
Run Code Online (Sandbox Code Playgroud)
Secure.cshtml
<h2>Secure</h2>
@Html.ActionLink("Logout", "Logout")
Run Code Online (Sandbox Code Playgroud)
我可能过度简化了这一点,但我读到的方式如下:
如果以上是正确的,您不需要会员提供者.[Authorize]属性只是查看表单身份验证cookie,以查看它是否已设置并且对cookie的当前生命周期有效.此身份验证cookie存储用户的用户名和cookie的过期时间(以及其他内容,但这里不重要).
鉴于此,您只需要设置web.config配置元素并设置一个方法来设置身份验证cookie.
Web.Config中
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)
登录URL GET操作
public ActionResult Logon(){
//if the user is logged in, send the to the home page
if(httpContext.User.Identity.IsAuthenticated_{
Return RedirectToAction("Index", "Home");
}
Return this.View(new LoginViewModel());
}
Run Code Online (Sandbox Code Playgroud)
登录URL POST操作
[HttpPost]
public ActionResult Logon(LoginViewModel model){
//Check for model errors
if(!ModelState.IsValid()){
Return this.View(model);
}
//Validate against web service - return error if false
if(!CheckClientsWebService(model.UserName, model.Password)){
ModelState.AddModelError("","The username or password is invalid");
Return this.View(model);
}
//Manually set the authentication cookie
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
//Send them on to the home page, they now have a authorization cookie
Return RedirectToAction("Index", "Home");
}
Run Code Online (Sandbox Code Playgroud)
调用该.SetAuthCookie()
函数后,用户现在将拥有一个身份验证票证,HttpContext.User.Identity.IsAuthenticated
只要cookie未过期,您就可以从中获取用户名HttpContext.User.Identity.Name
正如 Wiktor 评论的那样,实现您自己的MembershipProvider。只需实现您需要的方法,剩下的就扔掉NotImplementedException
.
在你的情况下,看起来你需要实现的只是public bool ValidateUser(string username, string password)
- 其实现只需要调用你的网络服务。
然后您可以使用所有标准的内置身份验证和授权内容。
归档时间: |
|
查看次数: |
12315 次 |
最近记录: |